Skip to content

Commit

Permalink
Replace std::env::home_dir with dirs::home_dir (#9077)
Browse files Browse the repository at this point in the history
`std::env::home_dir` is deprecated but will probably take a while until
it is deprecated on stable. For more info see rust-lang/rust#51656
  • Loading branch information
niklasad1 authored and 5chdn committed Jul 9, 2018
1 parent 76f7b7f commit e0dedc6
Show file tree
Hide file tree
Showing 7 changed files with 51 additions and 30 deletions.
30 changes: 22 additions & 8 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 3 additions & 4 deletions parity/upgrade.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,12 @@
//! Parity upgrade logic
use semver::{Version, SemVerError};
use std::collections::*;
use std::collections::HashMap;
use std::fs::{self, File, create_dir_all};
use std::env;
use std::io;
use std::io::{Read, Write};
use std::path::{PathBuf, Path};
use dir::{DatabaseDirectories, default_data_path};
use dir::{DatabaseDirectories, default_data_path, home_dir};
use dir::helpers::replace_home;
use journaldb::Algorithm;

Expand Down Expand Up @@ -106,7 +105,7 @@ fn with_locked_version<F>(db_path: Option<&str>, script: F) -> Result<usize, Err
where F: Fn(&Version) -> Result<usize, Error>
{
let mut path = db_path.map_or({
let mut path = env::home_dir().expect("Applications should have a home dir");
let mut path = home_dir().expect("Applications should have a home dir");
path.push(".parity");
path
}, PathBuf::from);
Expand Down
4 changes: 3 additions & 1 deletion util/dir/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
[package]
name = "dir"
version = "0.1.0"
version = "0.1.1"
authors = ["Parity Technologies <[email protected]>"]
license = "GPL3"

[dependencies]
ethereum-types = "0.3"
journaldb = { path = "../journaldb" }
app_dirs = { git = "https://github.com/paritytech/app-dirs-rs" }
dirs = "1.0.2"
3 changes: 1 addition & 2 deletions util/dir/src/helpers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,11 @@
// along with Parity. If not, see <http://www.gnu.org/licenses/>.

//! Directory helper functions
use std::env;
/// Replaces `$HOME` str with home directory path.
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 = arg.replace("$HOME", ::dirs::home_dir().unwrap().to_str().unwrap());
let r = r.replace("$BASE", base);
r.replace("/", &::std::path::MAIN_SEPARATOR.to_string())
}
Expand Down
23 changes: 13 additions & 10 deletions util/dir/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,12 @@

//! Dir utilities for platform-specific operations
extern crate app_dirs;
extern crate dirs;
extern crate ethereum_types;
extern crate journaldb;

pub mod helpers;
use std::{env, fs};
use std::fs;
use std::path::{PathBuf, Path};
use ethereum_types::{H64, H256};
use journaldb::Algorithm;
Expand All @@ -31,19 +32,21 @@ use app_dirs::{AppInfo, get_app_root, AppDataType};
// re-export platform-specific functions
use platform::*;

pub use dirs::home_dir;

/// Platform-specific chains path - Windows only
#[cfg(target_os = "windows")] pub const CHAINS_PATH: &'static str = "$LOCAL/chains";
#[cfg(target_os = "windows")] pub const CHAINS_PATH: &str = "$LOCAL/chains";
/// Platform-specific chains path
#[cfg(not(target_os = "windows"))] pub const CHAINS_PATH: &'static str = "$BASE/chains";
#[cfg(not(target_os = "windows"))] pub const CHAINS_PATH: &str = "$BASE/chains";

/// Platform-specific cache path - Windows only
#[cfg(target_os = "windows")] pub const CACHE_PATH: &'static str = "$LOCAL/cache";
#[cfg(target_os = "windows")] pub const CACHE_PATH: &str = "$LOCAL/cache";
/// Platform-specific cache path
#[cfg(not(target_os = "windows"))] pub const CACHE_PATH: &'static str = "$BASE/cache";
#[cfg(not(target_os = "windows"))] pub const CACHE_PATH: &str = "$BASE/cache";

// 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";
const LEGACY_CLIENT_DB_VER_STR: &str = "5.3";

#[derive(Debug, PartialEq)]
/// Parity local data directories
Expand Down Expand Up @@ -239,7 +242,7 @@ pub fn default_hypervisor_path() -> PathBuf {

/// Get home directory.
fn home() -> PathBuf {
env::home_dir().expect("Failed to get home dir")
dirs::home_dir().expect("Failed to get home dir")
}

/// Geth path
Expand Down Expand Up @@ -312,9 +315,9 @@ mod platform {
#[cfg(not(any(target_os = "macos", windows)))]
mod platform {
use std::path::PathBuf;
pub const AUTHOR: &'static str = "parity";
pub const PRODUCT: &'static str = "io.parity.ethereum";
pub const PRODUCT_HYPERVISOR: &'static str = "io.parity.ethereum-updates";
pub const AUTHOR: &str = "parity";
pub const PRODUCT: &str = "io.parity.ethereum";
pub const PRODUCT_HYPERVISOR: &str = "io.parity.ethereum-updates";

pub fn parity_base() -> PathBuf {
let mut home = super::home();
Expand Down
6 changes: 4 additions & 2 deletions util/path/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
[package]
name = "path"
version = "0.1.0"
authors = ["debris <[email protected]>"]
version = "0.1.1"
authors = ["Parity Technologies <[email protected]>"]
license = "GPL3"

[dependencies]
dirs = "1.0.2"
8 changes: 5 additions & 3 deletions util/path/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,16 @@
// along with Parity. If not, see <http://www.gnu.org/licenses/>.

//! Path utilities
extern crate dirs;

use std::path::Path;
use std::path::PathBuf;

#[cfg(target_os = "macos")]
/// Get the config path for application `name`.
/// `name` should be capitalized, e.g. `"Ethereum"`, `"Parity"`.
pub fn config_path(name: &str) -> PathBuf {
let mut home = ::std::env::home_dir().expect("Failed to get home dir");
let mut home = dirs::home_dir().expect("Failed to get home dir");
home.push("Library");
home.push(name);
home
Expand All @@ -32,7 +34,7 @@ pub fn config_path(name: &str) -> PathBuf {
/// Get the config path for application `name`.
/// `name` should be capitalized, e.g. `"Ethereum"`, `"Parity"`.
pub fn config_path(name: &str) -> PathBuf {
let mut home = ::std::env::home_dir().expect("Failed to get home dir");
let mut home = dirs::home_dir().expect("Failed to get home dir");
home.push("AppData");
home.push("Roaming");
home.push(name);
Expand All @@ -43,7 +45,7 @@ pub fn config_path(name: &str) -> PathBuf {
/// Get the config path for application `name`.
/// `name` should be capitalized, e.g. `"Ethereum"`, `"Parity"`.
pub fn config_path(name: &str) -> PathBuf {
let mut home = ::std::env::home_dir().expect("Failed to get home dir");
let mut home = dirs::home_dir().expect("Failed to get home dir");
home.push(format!(".{}", name.to_lowercase()));
home
}
Expand Down

0 comments on commit e0dedc6

Please sign in to comment.