From 09972471be8e1fa29ba2c58cb22831742c49ad21 Mon Sep 17 00:00:00 2001 From: Robert Collins Date: Mon, 8 Jun 2020 10:35:01 +1200 Subject: [PATCH] Abstract over home:: The home crate also accesses global process state, so we need to abstract over it as well. --- Cargo.lock | 2 -- Cargo.toml | 3 +++ src/currentprocess.rs | 1 + src/currentprocess/homethunk.rs | 21 +++++++++++++++++++++ src/utils/utils.rs | 6 +++--- 5 files changed, 28 insertions(+), 5 deletions(-) create mode 100644 src/currentprocess/homethunk.rs diff --git a/Cargo.lock b/Cargo.lock index 14c9e776db..1f981ea6fb 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -733,8 +733,6 @@ checksum = "644f9158b2f133fd50f5fb3242878846d9eb792e445c893805ff0e3824006e35" [[package]] name = "home" version = "0.5.3" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "2456aef2e6b6a9784192ae780c0f15bc57df0e918585282325e8c8ac27737654" dependencies = [ "winapi 0.3.8", ] diff --git a/Cargo.toml b/Cargo.toml index 86b11f531e..9f3b833d43 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -113,3 +113,6 @@ codegen-units = 1 # Reduce build time by setting proc-macro crates non optimized. [profile.release.build-override] opt-level = 0 + +[patch.crates-io] +home = { path = "../rustup-home" } \ No newline at end of file diff --git a/src/currentprocess.rs b/src/currentprocess.rs index da343e7719..dc06b9be68 100644 --- a/src/currentprocess.rs +++ b/src/currentprocess.rs @@ -14,6 +14,7 @@ use rand::{thread_rng, Rng}; pub mod argsource; pub mod cwdsource; pub mod filesource; +mod homethunk; pub mod varsource; use argsource::*; diff --git a/src/currentprocess/homethunk.rs b/src/currentprocess/homethunk.rs new file mode 100644 index 0000000000..9232877969 --- /dev/null +++ b/src/currentprocess/homethunk.rs @@ -0,0 +1,21 @@ +/// Adapts currentprocess to the trait home::Env +use std::ffi::OsString; +use std::io; +use std::ops::Deref; +use std::path::PathBuf; + +use super::CurrentDirSource; +use super::CurrentProcess; +use super::VarSource; + +impl home::Env for Box { + fn home_dir(&self) -> Option { + self.var("HOME").ok().map(|v| v.into()) + } + fn current_dir(&self) -> Result { + CurrentDirSource::current_dir(self.deref()) + } + fn var_os(&self, key: &str) -> Option { + VarSource::var_os(self.deref(), key) + } +} diff --git a/src/utils/utils.rs b/src/utils/utils.rs index 6955c3298e..d0d6508881 100644 --- a/src/utils/utils.rs +++ b/src/utils/utils.rs @@ -466,11 +466,11 @@ pub fn to_absolute>(path: P) -> Result { } pub fn home_dir() -> Option { - home::home_dir() + home::home_dir_from(&process()) } pub fn cargo_home() -> Result { - home::cargo_home().map_err(|e| Error::from_kind(ErrorKind::Io(e))) + home::cargo_home_from(&process()).map_err(|e| Error::from_kind(ErrorKind::Io(e))) } // Creates a ~/.rustup folder @@ -496,7 +496,7 @@ pub fn rustup_home_in_user_dir() -> Result { } pub fn rustup_home() -> Result { - home::rustup_home().map_err(|e| Error::from_kind(ErrorKind::Io(e))) + home::rustup_home_from(&process()).map_err(|e| Error::from_kind(ErrorKind::Io(e))) } pub fn format_path_for_display(path: &str) -> String {