From a076ffaf8c6117d95a36733dc20ab7b5738cea12 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tomasz=20Drwi=C4=99ga?= Date: Thu, 5 Jan 2017 14:12:54 +0100 Subject: [PATCH] Using local path on Windows (#4017) * Using non-roaming path on Windows * Fixing test --- parity/cli/mod.rs | 6 +++--- parity/cli/usage.txt | 2 +- parity/configuration.rs | 11 ++++++++--- parity/dir.rs | 21 +++++++++++++++++---- parity/helpers.rs | 9 +++++++-- 5 files changed, 36 insertions(+), 13 deletions(-) diff --git a/parity/cli/mod.rs b/parity/cli/mod.rs index 47a0af0bb..3515d0d35 100644 --- a/parity/cli/mod.rs +++ b/parity/cli/mod.rs @@ -16,7 +16,7 @@ #[macro_use] mod usage; -use dir::default_data_path; +use dir; usage! { { @@ -90,8 +90,8 @@ usage! { flag_no_download: bool = false, or |c: &Config| otry!(c.parity).no_download.clone(), flag_no_consensus: bool = false, or |c: &Config| otry!(c.parity).no_consensus.clone(), flag_chain: String = "homestead", or |c: &Config| otry!(c.parity).chain.clone(), - flag_base_path: String = default_data_path(), or |c: &Config| otry!(c.parity).base_path.clone(), - flag_db_path: String = "$BASE/chains", or |c: &Config| otry!(c.parity).db_path.clone(), + flag_base_path: String = dir::default_data_path(), or |c: &Config| otry!(c.parity).base_path.clone(), + flag_db_path: String = dir::CHAINS_PATH, or |c: &Config| otry!(c.parity).db_path.clone(), flag_keys_path: String = "$BASE/keys", or |c: &Config| otry!(c.parity).keys_path.clone(), flag_identity: String = "", or |c: &Config| otry!(c.parity).identity.clone(), diff --git a/parity/cli/usage.txt b/parity/cli/usage.txt index 7c3e5cc7d..9705c7359 100644 --- a/parity/cli/usage.txt +++ b/parity/cli/usage.txt @@ -336,7 +336,7 @@ Legacy Options: testnet --keys-path $HOME/parity/testnet-keys. Overrides the --keys-path option. --import-geth-keys Attempt to import keys from Geth client. - --datadir PATH Equivalent to --db-path PATH. + --datadir PATH Equivalent to --base-path PATH. --networkid INDEX Equivalent to --network-id INDEX. --peers NUM Equivalent to --min-peers NUM. --nodekey KEY Equivalent to --node-key KEY. diff --git a/parity/configuration.rs b/parity/configuration.rs index 85ff61d5c..f3135e0ee 100644 --- a/parity/configuration.rs +++ b/parity/configuration.rs @@ -30,11 +30,11 @@ use ethcore::verification::queue::VerifierSettings; use rpc::{IpcConfiguration, HttpConfiguration}; use ethcore_rpc::NetworkSettings; use cache::CacheConfig; -use helpers::{to_duration, to_mode, to_block_id, to_u256, to_pending_set, to_price, replace_home, +use helpers::{to_duration, to_mode, to_block_id, to_u256, to_pending_set, to_price, replace_home, replace_home_for_db, geth_ipc_path, parity_ipc_path, to_bootnodes, to_addresses, to_address, to_gas_limit, to_queue_strategy}; use params::{ResealPolicy, AccountsConfig, GasPricerConfig, MinerExtras}; use ethcore_logger::Config as LogConfig; -use dir::{Directories, default_hypervisor_path}; +use dir::{Directories, default_hypervisor_path, default_local_path}; use dapps::Configuration as DappsConfiguration; use signer::{Configuration as SignerConfiguration}; use updater::{UpdatePolicy, UpdateFilter, ReleaseTrack}; @@ -707,9 +707,14 @@ impl Configuration { fn directories(&self) -> Directories { use util::path; + let local_path = default_local_path(); let data_path = replace_home("", self.args.flag_datadir.as_ref().unwrap_or(&self.args.flag_base_path)); - let db_path = replace_home(&data_path, &self.args.flag_db_path); + let db_path = if self.args.flag_datadir.is_some() { + replace_home(&data_path, &self.args.flag_db_path) + } else { + replace_home_for_db(&data_path, &local_path, &self.args.flag_db_path) + }; let keys_path = replace_home(&data_path, &self.args.flag_keys_path); let dapps_path = replace_home(&data_path, &self.args.flag_dapps_path); let ui_path = replace_home(&data_path, &self.args.flag_ui_path); diff --git a/parity/dir.rs b/parity/dir.rs index bd83ef3c0..ffef0d94e 100644 --- a/parity/dir.rs +++ b/parity/dir.rs @@ -18,7 +18,7 @@ use std::fs; use std::path::{PathBuf, Path}; use util::{H64, H256}; use util::journaldb::Algorithm; -use helpers::replace_home; +use helpers::{replace_home, replace_home_for_db}; use app_dirs::{AppInfo, get_app_root, AppDataType}; #[cfg(target_os = "macos")] const AUTHOR: &'static str = "Parity"; @@ -31,6 +31,9 @@ use app_dirs::{AppInfo, get_app_root, AppDataType}; #[cfg(not(any(target_os = "windows", target_os = "macos")))] const PRODUCT: &'static str = "io.parity.ethereum"; #[cfg(not(any(target_os = "windows", target_os = "macos")))] const PRODUCT_HYPERVISOR: &'static str = "io.parity.ethereum-updates"; +#[cfg(target_os = "windows")] pub const CHAINS_PATH: &'static str = "$LOCAL/chains"; +#[cfg(not(target_os = "windows"))] pub const CHAINS_PATH: &'static str = "$BASE/chains"; + // this const is irrelevent cause we do have migrations now, // but we still use it for backwards compatibility const LEGACY_CLIENT_DB_VER_STR: &'static str = "5.3"; @@ -47,9 +50,10 @@ pub struct Directories { impl Default for Directories { fn default() -> Self { let data_dir = default_data_path(); + let local_dir = default_local_path(); Directories { base: replace_home(&data_dir, "$BASE"), - db: replace_home(&data_dir, "$BASE/chains"), + db: replace_home_for_db(&data_dir, &local_dir, CHAINS_PATH), keys: replace_home(&data_dir, "$BASE/keys"), signer: replace_home(&data_dir, "$BASE/signer"), dapps: replace_home(&data_dir, "$BASE/dapps"), @@ -209,6 +213,11 @@ pub fn default_data_path() -> String { get_app_root(AppDataType::UserData, &app_info).map(|p| p.to_string_lossy().into_owned()).unwrap_or_else(|_| "$HOME/.parity".to_owned()) } +pub fn default_local_path() -> String { + let app_info = AppInfo { name: PRODUCT, author: AUTHOR }; + get_app_root(AppDataType::UserCache, &app_info).map(|p| p.to_string_lossy().into_owned()).unwrap_or_else(|_| "$HOME/.parity".to_owned()) +} + pub fn default_hypervisor_path() -> String { let app_info = AppInfo { name: PRODUCT_HYPERVISOR, author: AUTHOR }; get_app_root(AppDataType::UserData, &app_info).map(|p| p.to_string_lossy().into_owned()).unwrap_or_else(|_| "$HOME/.parity-hypervisor".to_owned()) @@ -217,14 +226,18 @@ pub fn default_hypervisor_path() -> String { #[cfg(test)] mod tests { use super::Directories; - use helpers::replace_home; + use helpers::{replace_home, replace_home_for_db}; #[test] fn test_default_directories() { let data_dir = super::default_data_path(); + let local_dir = super::default_local_path(); let expected = Directories { base: replace_home(&data_dir, "$BASE"), - db: replace_home(&data_dir, "$BASE/chains"), + db: replace_home_for_db(&data_dir, &local_dir, + if cfg!(target_os = "windows") { "$LOCAL/chains" } + else { "$BASE/chains" } + ), keys: replace_home(&data_dir, "$BASE/keys"), signer: replace_home(&data_dir, "$BASE/signer"), dapps: replace_home(&data_dir, "$BASE/dapps"), diff --git a/parity/helpers.rs b/parity/helpers.rs index b91b62f1d..680306684 100644 --- a/parity/helpers.rs +++ b/parity/helpers.rs @@ -135,8 +135,13 @@ pub fn to_price(s: &str) -> Result { pub fn replace_home(base: &str, arg: &str) -> String { // the $HOME directory on mac os should be `~/Library` or `~/Library/Application Support` let r = arg.replace("$HOME", env::home_dir().unwrap().to_str().unwrap()); - let r = r.replace("$BASE", base ); - r.replace("/", &::std::path::MAIN_SEPARATOR.to_string() ) + let r = r.replace("$BASE", base); + r.replace("/", &::std::path::MAIN_SEPARATOR.to_string()) +} + +pub fn replace_home_for_db(base: &str, local: &str, arg: &str) -> String { + let r = replace_home(base, arg); + r.replace("$LOCAL", local) } /// Flush output buffer.