Skip to content
This repository has been archived by the owner on Nov 6, 2020. It is now read-only.

Commit

Permalink
Merge pull request #1083 from ethcore/path-man
Browse files Browse the repository at this point in the history
path centralized
  • Loading branch information
NikVolf committed May 14, 2016
2 parents 354ac7d + 3ea26fc commit 05f00a9
Show file tree
Hide file tree
Showing 4 changed files with 102 additions and 45 deletions.
64 changes: 43 additions & 21 deletions parity/configuration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -60,11 +65,6 @@ impl Configuration {
self.args.flag_maxpeers.unwrap_or(self.args.flag_peers) as u32
}

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())
}

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(|_| {
Expand Down Expand Up @@ -113,14 +113,6 @@ impl Configuration {
}
}

pub fn keys_path(&self) -> String {
self.args.flag_keys_path.replace("$HOME", env::home_dir().unwrap().to_str().unwrap())
}

pub fn keys_iterations(&self) -> u32 {
self.args.flag_keys_iterations
}

pub fn spec(&self) -> Spec {
match self.chain().as_str() {
"frontier" | "homestead" | "mainnet" => ethereum::new_frontier(),
Expand Down Expand Up @@ -272,19 +264,18 @@ impl Configuration {
cors.map_or_else(Vec::new, |c| c.split(',').map(|s| s.to_owned()).collect())
}

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()
}

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),
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()),
}
}
Expand All @@ -301,6 +292,37 @@ 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("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,
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)]
Expand Down
26 changes: 2 additions & 24 deletions util/src/keys/geth_import.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Vec<(Address, String)>, ImportError> {
Expand Down Expand Up @@ -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)]
Expand Down
1 change: 1 addition & 0 deletions util/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::*;
Expand Down
56 changes: 56 additions & 0 deletions util/src/path.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
// 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 <http://www.gnu.org/licenses/>.

//! 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 is 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
}
}

0 comments on commit 05f00a9

Please sign in to comment.