From d5d5b0918cca6c4cc9b61f271ad3fd7295c9f72a Mon Sep 17 00:00:00 2001 From: Nikolay Volf Date: Fri, 13 May 2016 13:11:50 +0300 Subject: [PATCH 1/5] mod for paths & ethereum default paths --- util/src/keys/geth_import.rs | 26 ++-------------- util/src/lib.rs | 1 + util/src/path.rs | 57 ++++++++++++++++++++++++++++++++++++ 3 files changed, 60 insertions(+), 24 deletions(-) create mode 100644 util/src/path.rs diff --git a/util/src/keys/geth_import.rs b/util/src/keys/geth_import.rs index 9886a61a8..56e73f790 100644 --- a/util/src/keys/geth_import.rs +++ b/util/src/keys/geth_import.rs @@ -20,6 +20,7 @@ use common::*; use keys::store::SecretStore; use keys::directory::KeyFileContent; use std::path::PathBuf; +use path; /// Enumerates all geth keys in the directory and returns collection of tuples `(accountId, filename)` pub fn enumerate_geth_keys(path: &Path) -> Result, ImportError> { @@ -98,30 +99,7 @@ pub fn import_geth_keys(secret_store: &mut SecretStore, geth_keyfiles_directory: /// /// Based on https://github.com/ethereum/go-ethereum/blob/e553215/common/path.go#L75 pub fn keystore_dir() -> PathBuf { - #[cfg(target_os = "macos")] - fn data_dir(mut home: PathBuf) -> PathBuf { - home.push("Library"); - home.push("Ethereum"); - home - } - - #[cfg(windows)] - fn data_dir(mut home: PathBuf) -> PathBuf { - home.push("AppData"); - home.push("Roaming"); - home.push("Ethereum"); - home - } - - #[cfg(not(any(target_os = "macos", windows)))] - fn data_dir(mut home: PathBuf) -> PathBuf { - home.push(".ethereum"); - home - } - - let mut data_dir = data_dir(::std::env::home_dir().expect("Failed to get home dir")); - data_dir.push("keystore"); - data_dir + path::ethereum::with_default("keystore") } #[cfg(test)] diff --git a/util/src/lib.rs b/util/src/lib.rs index 530f2b4c5..a97282198 100644 --- a/util/src/lib.rs +++ b/util/src/lib.rs @@ -148,6 +148,7 @@ pub mod panics; pub mod keys; pub mod table; pub mod network_settings; +pub mod path; pub use common::*; pub use misc::*; diff --git a/util/src/path.rs b/util/src/path.rs new file mode 100644 index 000000000..10aaca44a --- /dev/null +++ b/util/src/path.rs @@ -0,0 +1,57 @@ +// Copyright 2015, 2016 Ethcore (UK) Ltd. +// This file is part of Parity. + +// Parity is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. + +// Parity is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. + +// You should have received a copy of the GNU General Public License +// along with Parity. If not, see . + +//! Path utilities + +/// Default ethereum paths +pub mod ethereum { + use std::path::PathBuf; + + #[cfg(target_os = "macos")] + /// Default path for ethereum installation on Mac Os + pub fn default() -> PathBuf { + let mut home = ::std::env::home_dir().expect("Failed to get home dir"); + home.push("Library"); + home.push("Ethereum"); + home + } + + #[cfg(windows)] + /// Default path for ethereum installation on Windows + pub fn default() -> PathBuf { + let mut home = ::std::env::home_dir().expect("Failed to get home dir"); + home.push("AppData"); + home.push("Roaming"); + home.push("Ethereum"); + home + } + + #[cfg(not(any(target_os = "macos", windows)))] + /// Default path for ethereum installation on posix system which and not Mac OS + pub fn default() -> PathBuf { + let mut home = ::std::env::home_dir().expect("Failed to get home dir"); + home.push(".ethereum"); + home + } + + /// Get the specific folder inside default ethereum installation + pub fn with_default(s: &str) -> PathBuf { + let mut pth = default(); + pth.push(s); + pth + } +} + From 9150538ac4503db1a691fba97ba226168ad570a7 Mon Sep 17 00:00:00 2001 From: Nikolay Volf Date: Fri, 13 May 2016 13:53:33 +0300 Subject: [PATCH 2/5] refactoring in configuration --- parity/configuration.rs | 27 +++++++++++++++------------ util/src/path.rs | 3 +-- 2 files changed, 16 insertions(+), 14 deletions(-) diff --git a/parity/configuration.rs b/parity/configuration.rs index 8d0eea9bb..90b5b3d1d 100644 --- a/parity/configuration.rs +++ b/parity/configuration.rs @@ -61,8 +61,7 @@ impl Configuration { } pub fn path(&self) -> String { - let d = self.args.flag_datadir.as_ref().unwrap_or(&self.args.flag_db_path); - d.replace("$HOME", env::home_dir().unwrap().to_str().unwrap()) + Configuration::replace_home(&self.args.flag_datadir.as_ref().unwrap_or(&self.args.flag_db_path)) } pub fn author(&self) -> Address { @@ -114,7 +113,7 @@ impl Configuration { } pub fn keys_path(&self) -> String { - self.args.flag_keys_path.replace("$HOME", env::home_dir().unwrap().to_str().unwrap()) + Configuration::replace_home(&self.args.flag_keys_path) } pub fn spec(&self) -> Spec { @@ -266,20 +265,24 @@ impl Configuration { pub fn rpc_cors(&self) -> Option { self.args.flag_jsonrpc_cors.clone().or(self.args.flag_rpccorsdomain.clone()) } - - fn geth_ipc_path() -> &'static str { - if cfg!(target_os = "macos") { - "$HOME/Library/Ethereum/geth.ipc" - } else { - "$HOME/.ethereum/geth.ipc" - } + + fn geth_ipc_path() -> String { + path::ethereum::with_default("geth.ipc").to_str().unwrap().to_owned() + } + + fn replace_home(arg: &str) -> String { + arg.replace("$HOME", env::home_dir().unwrap().to_str().unwrap()) + } + + fn ipc_path(&self) -> String { + if self.args.flag_geth { Self::geth_ipc_path() } + else { Configuration::replace_home(&self.args.flag_ipcpath.clone().unwrap_or(self.args.flag_ipc_path.clone())) } } pub fn ipc_settings(&self) -> IpcConfiguration { IpcConfiguration { enabled: !(self.args.flag_ipcdisable || self.args.flag_ipc_off), - socket_addr: if self.args.flag_geth { Self::geth_ipc_path().to_owned() } else { self.args.flag_ipcpath.clone().unwrap_or(self.args.flag_ipc_path.clone()) } - .replace("$HOME", env::home_dir().unwrap().to_str().unwrap()), + socket_addr: self.ipc_path(), apis: self.args.flag_ipcapi.clone().unwrap_or(self.args.flag_ipc_apis.clone()), } } diff --git a/util/src/path.rs b/util/src/path.rs index 10aaca44a..3a8dcaaae 100644 --- a/util/src/path.rs +++ b/util/src/path.rs @@ -40,7 +40,7 @@ pub mod ethereum { } #[cfg(not(any(target_os = "macos", windows)))] - /// Default path for ethereum installation on posix system which and not Mac OS + /// Default path for ethereum installation on posix system which is not Mac OS pub fn default() -> PathBuf { let mut home = ::std::env::home_dir().expect("Failed to get home dir"); home.push(".ethereum"); @@ -54,4 +54,3 @@ pub mod ethereum { pth } } - From 7c19930efa01c391262b320a34d711114d594957 Mon Sep 17 00:00:00 2001 From: Nikolay Volf Date: Fri, 13 May 2016 18:32:32 +0300 Subject: [PATCH 3/5] creating all in one place --- parity/configuration.rs | 52 +++++++++++++++++++++++++++-------------- 1 file changed, 35 insertions(+), 17 deletions(-) diff --git a/parity/configuration.rs b/parity/configuration.rs index 90b5b3d1d..ed28d0725 100644 --- a/parity/configuration.rs +++ b/parity/configuration.rs @@ -37,6 +37,11 @@ pub struct Configuration { pub args: Args } +pub struct Directories { + pub keys: String, + pub db: String, +} + impl Configuration { pub fn parse() -> Self { Configuration { @@ -60,10 +65,6 @@ impl Configuration { self.args.flag_maxpeers.unwrap_or(self.args.flag_peers) as u32 } - pub fn path(&self) -> String { - Configuration::replace_home(&self.args.flag_datadir.as_ref().unwrap_or(&self.args.flag_db_path)) - } - pub fn author(&self) -> Address { let d = self.args.flag_etherbase.as_ref().unwrap_or(&self.args.flag_author); Address::from_str(clean_0x(d)).unwrap_or_else(|_| { @@ -112,10 +113,6 @@ impl Configuration { } } - pub fn keys_path(&self) -> String { - Configuration::replace_home(&self.args.flag_keys_path) - } - pub fn spec(&self) -> Spec { match self.chain().as_str() { "frontier" | "homestead" | "mainnet" => ethereum::new_frontier(), @@ -270,15 +267,6 @@ impl Configuration { path::ethereum::with_default("geth.ipc").to_str().unwrap().to_owned() } - fn replace_home(arg: &str) -> String { - arg.replace("$HOME", env::home_dir().unwrap().to_str().unwrap()) - } - - fn ipc_path(&self) -> String { - if self.args.flag_geth { Self::geth_ipc_path() } - else { Configuration::replace_home(&self.args.flag_ipcpath.clone().unwrap_or(self.args.flag_ipc_path.clone())) } - } - pub fn ipc_settings(&self) -> IpcConfiguration { IpcConfiguration { enabled: !(self.args.flag_ipcdisable || self.args.flag_ipc_off), @@ -299,6 +287,36 @@ impl Configuration { rpc_port: self.args.flag_rpcport.unwrap_or(self.args.flag_jsonrpc_port), } } + + pub fn directories(&self) -> Directories { + let db_path = Configuration::replace_home( + &self.args.flag_datadir.as_ref().unwrap_or(&self.args.flag_db_path)); + std::fs::create_dir_all(db_path).unwrap_or_else(|e| die_with_io_error(e)); + + let keys_path = Configuration::replace_home(&self.args.flag_keys_path); + + Directories { + keys: keys_path, + db: db_path, + } + } + + pub fn keys_path(&self) -> String { + self.directories().keys + } + + pub fn path(&self) -> String { + self.directories().db + } + + fn replace_home(arg: &str) -> String { + arg.replace("$HOME", env::home_dir().unwrap().to_str().unwrap()) + } + + fn ipc_path(&self) -> String { + if self.args.flag_geth { Self::geth_ipc_path() } + else { Configuration::replace_home(&self.args.flag_ipcpath.clone().unwrap_or(self.args.flag_ipc_path.clone())) } + } } #[cfg(test)] From 96496d634953d8de4838608bc24b4ba04960eb34 Mon Sep 17 00:00:00 2001 From: Nikolay Volf Date: Fri, 13 May 2016 18:36:18 +0300 Subject: [PATCH 4/5] by ref --- parity/configuration.rs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/parity/configuration.rs b/parity/configuration.rs index ed28d0725..920cb7eed 100644 --- a/parity/configuration.rs +++ b/parity/configuration.rs @@ -291,9 +291,10 @@ impl Configuration { pub fn directories(&self) -> Directories { let db_path = Configuration::replace_home( &self.args.flag_datadir.as_ref().unwrap_or(&self.args.flag_db_path)); - std::fs::create_dir_all(db_path).unwrap_or_else(|e| die_with_io_error(e)); + ::std::fs::create_dir_all(&db_path).unwrap_or_else(|e| die_with_io_error("main", e)); let keys_path = Configuration::replace_home(&self.args.flag_keys_path); + ::std::fs::create_dir_all(&db_path).unwrap_or_else(|e| die_with_io_error("main", e)); Directories { keys: keys_path, From 3ea26fcb0a5f699b7695c8c692b5475954f4d98c Mon Sep 17 00:00:00 2001 From: Nikolay Volf Date: Sat, 14 May 2016 20:43:29 +0300 Subject: [PATCH 5/5] merged out function return --- parity/configuration.rs | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/parity/configuration.rs b/parity/configuration.rs index 2f2a1cc74..67fc30f32 100644 --- a/parity/configuration.rs +++ b/parity/configuration.rs @@ -268,6 +268,10 @@ impl Configuration { path::ethereum::with_default("geth.ipc").to_str().unwrap().to_owned() } + pub fn keys_iterations(&self) -> u32 { + self.args.flag_keys_iterations + } + pub fn ipc_settings(&self) -> IpcConfiguration { IpcConfiguration { enabled: !(self.args.flag_ipcdisable || self.args.flag_ipc_off),