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

Avoid using $HOME if not necessary #9273

Merged
merged 2 commits into from
Aug 3, 2018
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion parity/upgrade.rs
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,9 @@ fn upgrade_from_version(previous_version: &Version) -> Result<usize, Error> {
fn with_locked_version<F>(db_path: Option<&str>, script: F) -> Result<usize, Error>
where F: Fn(&Version) -> Result<usize, Error>
{
let mut path = db_path.map_or({
let mut path = db_path.map_or_else(|| {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It seems to me like the db_path is always is_some() (with_locked_version is only used in upgrade, and db_path in upgrade is always Some). Maybe we can remove map_or_else altogether, or am I wrong?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think you're right.

// Note: this can panic on device such as Android where $HOME isn't set
// That's why the `map_or_else` is important compared to `map_or`.
let mut path = env::home_dir().expect("Applications should have a home dir");
path.push(".parity");
path
Expand Down
7 changes: 6 additions & 1 deletion util/dir/src/helpers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,12 @@ 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());
// We use an `if` so that we don't need to call `home_dir()` if not necessary.
let r = if arg.contains("$HOME") {
arg.replace("$HOME", env::home_dir().expect("$HOME isn't defined").to_str().unwrap())
} else {
arg.to_owned()
};
let r = r.replace("$BASE", base);
r.replace("/", &::std::path::MAIN_SEPARATOR.to_string())
}
Expand Down