Vendor things
This commit is contained in:
parent
5deceec006
commit
977e3c17e5
19434 changed files with 10682014 additions and 0 deletions
106
third-party/vendor/home/src/env.rs
vendored
Normal file
106
third-party/vendor/home/src/env.rs
vendored
Normal file
|
|
@ -0,0 +1,106 @@
|
|||
//! Lower-level utilities for mocking the process environment.
|
||||
|
||||
use std::{
|
||||
ffi::OsString,
|
||||
io,
|
||||
path::{Path, PathBuf},
|
||||
};
|
||||
|
||||
/// Permits parameterizing the home functions via the _from variants - used for
|
||||
/// in-process unit testing by rustup.
|
||||
pub trait Env {
|
||||
/// Return the path to the users home dir, or None if any error occurs:
|
||||
/// see home_inner.
|
||||
fn home_dir(&self) -> Option<PathBuf>;
|
||||
/// Return the current working directory.
|
||||
fn current_dir(&self) -> io::Result<PathBuf>;
|
||||
/// Get an environment variable, as per std::env::var_os.
|
||||
fn var_os(&self, key: &str) -> Option<OsString>;
|
||||
}
|
||||
|
||||
/// Implements Env for the OS context, both Unix style and Windows.
|
||||
///
|
||||
/// This is trait permits in-process testing by providing a control point to
|
||||
/// allow in-process divergence on what is normally process wide state.
|
||||
///
|
||||
/// Implementations should be provided by whatever testing framework the caller
|
||||
/// is using. Code that is not performing in-process threaded testing requiring
|
||||
/// isolated rustup/cargo directories does not need this trait or the _from
|
||||
/// functions.
|
||||
pub struct OsEnv;
|
||||
impl Env for OsEnv {
|
||||
fn home_dir(&self) -> Option<PathBuf> {
|
||||
crate::home_dir_inner()
|
||||
}
|
||||
fn current_dir(&self) -> io::Result<PathBuf> {
|
||||
std::env::current_dir()
|
||||
}
|
||||
fn var_os(&self, key: &str) -> Option<OsString> {
|
||||
std::env::var_os(key)
|
||||
}
|
||||
}
|
||||
|
||||
pub const OS_ENV: OsEnv = OsEnv {};
|
||||
|
||||
/// Returns the path of the current user's home directory from [`Env::home_dir`].
|
||||
pub fn home_dir_with_env(env: &dyn Env) -> Option<PathBuf> {
|
||||
env.home_dir()
|
||||
}
|
||||
|
||||
/// Variant of cargo_home where the environment source is parameterized. This is
|
||||
/// specifically to support in-process testing scenarios as environment
|
||||
/// variables and user home metadata are normally process global state. See the
|
||||
/// [`Env`] trait.
|
||||
pub fn cargo_home_with_env(env: &dyn Env) -> io::Result<PathBuf> {
|
||||
let cwd = env.current_dir()?;
|
||||
cargo_home_with_cwd_env(env, &cwd)
|
||||
}
|
||||
|
||||
/// Variant of cargo_home_with_cwd where the environment source is
|
||||
/// parameterized. This is specifically to support in-process testing scenarios
|
||||
/// as environment variables and user home metadata are normally process global
|
||||
/// state. See the OsEnv trait.
|
||||
pub fn cargo_home_with_cwd_env(env: &dyn Env, cwd: &Path) -> io::Result<PathBuf> {
|
||||
match env.var_os("CARGO_HOME").filter(|h| !h.is_empty()) {
|
||||
Some(home) => {
|
||||
let home = PathBuf::from(home);
|
||||
if home.is_absolute() {
|
||||
Ok(home)
|
||||
} else {
|
||||
Ok(cwd.join(&home))
|
||||
}
|
||||
}
|
||||
_ => home_dir_with_env(env)
|
||||
.map(|p| p.join(".cargo"))
|
||||
.ok_or_else(|| io::Error::new(io::ErrorKind::Other, "could not find cargo home dir")),
|
||||
}
|
||||
}
|
||||
|
||||
/// Variant of cargo_home_with_cwd where the environment source is
|
||||
/// parameterized. This is specifically to support in-process testing scenarios
|
||||
/// as environment variables and user home metadata are normally process global
|
||||
/// state. See the OsEnv trait.
|
||||
pub fn rustup_home_with_env(env: &dyn Env) -> io::Result<PathBuf> {
|
||||
let cwd = env.current_dir()?;
|
||||
rustup_home_with_cwd_env(env, &cwd)
|
||||
}
|
||||
|
||||
/// Variant of cargo_home_with_cwd where the environment source is
|
||||
/// parameterized. This is specifically to support in-process testing scenarios
|
||||
/// as environment variables and user home metadata are normally process global
|
||||
/// state. See the OsEnv trait.
|
||||
pub fn rustup_home_with_cwd_env(env: &dyn Env, cwd: &Path) -> io::Result<PathBuf> {
|
||||
match env.var_os("RUSTUP_HOME").filter(|h| !h.is_empty()) {
|
||||
Some(home) => {
|
||||
let home = PathBuf::from(home);
|
||||
if home.is_absolute() {
|
||||
Ok(home)
|
||||
} else {
|
||||
Ok(cwd.join(&home))
|
||||
}
|
||||
}
|
||||
_ => home_dir_with_env(env)
|
||||
.map(|d| d.join(".rustup"))
|
||||
.ok_or_else(|| io::Error::new(io::ErrorKind::Other, "could not find rustup home dir")),
|
||||
}
|
||||
}
|
||||
146
third-party/vendor/home/src/lib.rs
vendored
Normal file
146
third-party/vendor/home/src/lib.rs
vendored
Normal file
|
|
@ -0,0 +1,146 @@
|
|||
//! Canonical definitions of `home_dir`, `cargo_home`, and `rustup_home`.
|
||||
//!
|
||||
//! The definition of `home_dir` provided by the standard library is
|
||||
//! incorrect because it considers the `HOME` environment variable on
|
||||
//! Windows. This causes surprising situations where a Rust program
|
||||
//! will behave differently depending on whether it is run under a
|
||||
//! Unix emulation environment like Cygwin or MinGW. Neither Cargo nor
|
||||
//! rustup use the standard libraries definition - they use the
|
||||
//! definition here.
|
||||
//!
|
||||
//! This crate provides two additional functions, `cargo_home` and
|
||||
//! `rustup_home`, which are the canonical way to determine the
|
||||
//! location that Cargo and rustup use to store their data.
|
||||
//! The `env` module contains utilities for mocking the process environment
|
||||
//! by Cargo and rustup.
|
||||
//!
|
||||
//! See also this [discussion].
|
||||
//!
|
||||
//! [discussion]: https://github.com/rust-lang/rust/pull/46799#issuecomment-361156935
|
||||
|
||||
#![allow(clippy::disallowed_methods)]
|
||||
|
||||
pub mod env;
|
||||
|
||||
#[cfg(target_os = "windows")]
|
||||
mod windows;
|
||||
|
||||
use std::io;
|
||||
use std::path::{Path, PathBuf};
|
||||
|
||||
/// Returns the path of the current user's home directory using environment
|
||||
/// variables or OS-specific APIs.
|
||||
///
|
||||
/// # Unix
|
||||
///
|
||||
/// Returns the value of the `HOME` environment variable if it is set
|
||||
/// **even** if it is an empty string. Otherwise, it tries to determine the
|
||||
/// home directory by invoking the [`getpwuid_r`][getpwuid] function with
|
||||
/// the UID of the current user.
|
||||
///
|
||||
/// [getpwuid]: https://linux.die.net/man/3/getpwuid_r
|
||||
///
|
||||
/// # Windows
|
||||
///
|
||||
/// Returns the value of the `USERPROFILE` environment variable if it is set
|
||||
/// **and** it is not an empty string. Otherwise, it tries to determine the
|
||||
/// home directory by invoking the [`SHGetKnownFolderPath`][shgkfp] function with
|
||||
/// [`FOLDERID_Profile`][knownfolderid].
|
||||
///
|
||||
/// [shgkfp]: https://learn.microsoft.com/en-us/windows/win32/api/shlobj_core/nf-shlobj_core-shgetknownfolderpath
|
||||
/// [knownfolderid]: https://learn.microsoft.com/en-us/windows/win32/shell/knownfolderid
|
||||
///
|
||||
/// # Examples
|
||||
///
|
||||
/// ```
|
||||
/// match home::home_dir() {
|
||||
/// Some(path) if !path.as_os_str().is_empty() => println!("{}", path.display()),
|
||||
/// _ => println!("Unable to get your home dir!"),
|
||||
/// }
|
||||
/// ```
|
||||
pub fn home_dir() -> Option<PathBuf> {
|
||||
env::home_dir_with_env(&env::OS_ENV)
|
||||
}
|
||||
|
||||
#[cfg(windows)]
|
||||
use windows::home_dir_inner;
|
||||
|
||||
#[cfg(any(unix, target_os = "redox"))]
|
||||
fn home_dir_inner() -> Option<PathBuf> {
|
||||
#[allow(deprecated)]
|
||||
std::env::home_dir()
|
||||
}
|
||||
|
||||
/// Returns the storage directory used by Cargo, often knowns as
|
||||
/// `.cargo` or `CARGO_HOME`.
|
||||
///
|
||||
/// It returns one of the following values, in this order of
|
||||
/// preference:
|
||||
///
|
||||
/// - The value of the `CARGO_HOME` environment variable, if it is
|
||||
/// an absolute path.
|
||||
/// - The value of the current working directory joined with the value
|
||||
/// of the `CARGO_HOME` environment variable, if `CARGO_HOME` is a
|
||||
/// relative directory.
|
||||
/// - The `.cargo` directory in the user's home directory, as reported
|
||||
/// by the `home_dir` function.
|
||||
///
|
||||
/// # Errors
|
||||
///
|
||||
/// This function fails if it fails to retrieve the current directory,
|
||||
/// or if the home directory cannot be determined.
|
||||
///
|
||||
/// # Examples
|
||||
///
|
||||
/// ```
|
||||
/// match home::cargo_home() {
|
||||
/// Ok(path) => println!("{}", path.display()),
|
||||
/// Err(err) => eprintln!("Cannot get your cargo home dir: {:?}", err),
|
||||
/// }
|
||||
/// ```
|
||||
pub fn cargo_home() -> io::Result<PathBuf> {
|
||||
env::cargo_home_with_env(&env::OS_ENV)
|
||||
}
|
||||
|
||||
/// Returns the storage directory used by Cargo within `cwd`.
|
||||
/// For more details, see [`cargo_home`](fn.cargo_home.html).
|
||||
pub fn cargo_home_with_cwd(cwd: &Path) -> io::Result<PathBuf> {
|
||||
env::cargo_home_with_cwd_env(&env::OS_ENV, cwd)
|
||||
}
|
||||
|
||||
/// Returns the storage directory used by rustup, often knowns as
|
||||
/// `.rustup` or `RUSTUP_HOME`.
|
||||
///
|
||||
/// It returns one of the following values, in this order of
|
||||
/// preference:
|
||||
///
|
||||
/// - The value of the `RUSTUP_HOME` environment variable, if it is
|
||||
/// an absolute path.
|
||||
/// - The value of the current working directory joined with the value
|
||||
/// of the `RUSTUP_HOME` environment variable, if `RUSTUP_HOME` is a
|
||||
/// relative directory.
|
||||
/// - The `.rustup` directory in the user's home directory, as reported
|
||||
/// by the `home_dir` function.
|
||||
///
|
||||
/// # Errors
|
||||
///
|
||||
/// This function fails if it fails to retrieve the current directory,
|
||||
/// or if the home directory cannot be determined.
|
||||
///
|
||||
/// # Examples
|
||||
///
|
||||
/// ```
|
||||
/// match home::rustup_home() {
|
||||
/// Ok(path) => println!("{}", path.display()),
|
||||
/// Err(err) => eprintln!("Cannot get your rustup home dir: {:?}", err),
|
||||
/// }
|
||||
/// ```
|
||||
pub fn rustup_home() -> io::Result<PathBuf> {
|
||||
env::rustup_home_with_env(&env::OS_ENV)
|
||||
}
|
||||
|
||||
/// Returns the storage directory used by rustup within `cwd`.
|
||||
/// For more details, see [`rustup_home`](fn.rustup_home.html).
|
||||
pub fn rustup_home_with_cwd(cwd: &Path) -> io::Result<PathBuf> {
|
||||
env::rustup_home_with_cwd_env(&env::OS_ENV, cwd)
|
||||
}
|
||||
73
third-party/vendor/home/src/windows.rs
vendored
Normal file
73
third-party/vendor/home/src/windows.rs
vendored
Normal file
|
|
@ -0,0 +1,73 @@
|
|||
use std::env;
|
||||
use std::ffi::OsString;
|
||||
use std::os::windows::ffi::OsStringExt;
|
||||
use std::path::PathBuf;
|
||||
use std::ptr;
|
||||
use std::slice;
|
||||
|
||||
use windows_sys::Win32::Foundation::S_OK;
|
||||
use windows_sys::Win32::System::Com::CoTaskMemFree;
|
||||
use windows_sys::Win32::UI::Shell::{FOLDERID_Profile, SHGetKnownFolderPath, KF_FLAG_DONT_VERIFY};
|
||||
|
||||
pub fn home_dir_inner() -> Option<PathBuf> {
|
||||
env::var_os("USERPROFILE")
|
||||
.filter(|s| !s.is_empty())
|
||||
.map(PathBuf::from)
|
||||
.or_else(home_dir_crt)
|
||||
}
|
||||
|
||||
#[cfg(not(target_vendor = "uwp"))]
|
||||
fn home_dir_crt() -> Option<PathBuf> {
|
||||
unsafe {
|
||||
let mut path = ptr::null_mut();
|
||||
match SHGetKnownFolderPath(&FOLDERID_Profile, KF_FLAG_DONT_VERIFY as u32, 0, &mut path) {
|
||||
S_OK => {
|
||||
let path_slice = slice::from_raw_parts(path, wcslen(path));
|
||||
let s = OsString::from_wide(&path_slice);
|
||||
CoTaskMemFree(path.cast());
|
||||
Some(PathBuf::from(s))
|
||||
}
|
||||
_ => {
|
||||
// Free any allocated memory even on failure. A null ptr is a no-op for `CoTaskMemFree`.
|
||||
CoTaskMemFree(path.cast());
|
||||
None
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(target_vendor = "uwp")]
|
||||
fn home_dir_crt() -> Option<PathBuf> {
|
||||
None
|
||||
}
|
||||
|
||||
extern "C" {
|
||||
fn wcslen(buf: *const u16) -> usize;
|
||||
}
|
||||
|
||||
#[cfg(not(target_vendor = "uwp"))]
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::home_dir_inner;
|
||||
use std::env;
|
||||
use std::ops::Deref;
|
||||
use std::path::{Path, PathBuf};
|
||||
|
||||
#[test]
|
||||
fn test_with_without() {
|
||||
let olduserprofile = env::var_os("USERPROFILE").unwrap();
|
||||
|
||||
env::remove_var("HOME");
|
||||
env::remove_var("USERPROFILE");
|
||||
|
||||
assert_eq!(home_dir_inner(), Some(PathBuf::from(olduserprofile)));
|
||||
|
||||
let home = Path::new(r"C:\Users\foo tar baz");
|
||||
|
||||
env::set_var("HOME", home.as_os_str());
|
||||
assert_ne!(home_dir_inner().as_ref().map(Deref::deref), Some(home));
|
||||
|
||||
env::set_var("USERPROFILE", home.as_os_str());
|
||||
assert_eq!(home_dir_inner().as_ref().map(Deref::deref), Some(home));
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue