2017-12-17 15:21:47 +00:00
|
|
|
#![allow(dead_code)]
|
2019-12-21 11:16:18 +00:00
|
|
|
#![unstable(feature = "process_internals", issue = "none")]
|
2017-12-17 15:21:47 +00:00
|
|
|
|
2019-02-10 19:23:21 +00:00
|
|
|
use crate::collections::BTreeMap;
|
2019-09-04 02:32:44 +00:00
|
|
|
use crate::env;
|
|
|
|
use crate::ffi::{OsStr, OsString};
|
|
|
|
use crate::sys::process::EnvKey;
|
2017-12-17 15:21:47 +00:00
|
|
|
|
|
|
|
// Stores a set of changes to an environment
|
|
|
|
#[derive(Clone, Debug)]
|
2019-09-04 02:32:44 +00:00
|
|
|
pub struct CommandEnv {
|
2017-12-17 15:21:47 +00:00
|
|
|
clear: bool,
|
2018-03-19 22:40:09 +00:00
|
|
|
saw_path: bool,
|
2019-11-27 18:29:00 +00:00
|
|
|
vars: BTreeMap<EnvKey, Option<OsString>>,
|
2017-12-17 15:21:47 +00:00
|
|
|
}
|
|
|
|
|
2019-09-04 02:32:44 +00:00
|
|
|
impl Default for CommandEnv {
|
2017-12-17 15:21:47 +00:00
|
|
|
fn default() -> Self {
|
2019-11-27 18:29:00 +00:00
|
|
|
CommandEnv { clear: false, saw_path: false, vars: Default::default() }
|
2017-12-17 15:21:47 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-09-04 02:32:44 +00:00
|
|
|
impl CommandEnv {
|
2017-12-17 15:21:47 +00:00
|
|
|
// Capture the current environment with these changes applied
|
2019-09-04 02:32:44 +00:00
|
|
|
pub fn capture(&self) -> BTreeMap<EnvKey, OsString> {
|
|
|
|
let mut result = BTreeMap::<EnvKey, OsString>::new();
|
2017-12-17 15:21:47 +00:00
|
|
|
if !self.clear {
|
|
|
|
for (k, v) in env::vars_os() {
|
|
|
|
result.insert(k.into(), v);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for (k, maybe_v) in &self.vars {
|
|
|
|
if let &Some(ref v) = maybe_v {
|
|
|
|
result.insert(k.clone(), v.clone());
|
|
|
|
} else {
|
|
|
|
result.remove(k);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
result
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn is_unchanged(&self) -> bool {
|
|
|
|
!self.clear && self.vars.is_empty()
|
|
|
|
}
|
|
|
|
|
2019-09-04 02:32:44 +00:00
|
|
|
pub fn capture_if_changed(&self) -> Option<BTreeMap<EnvKey, OsString>> {
|
2019-11-27 18:29:00 +00:00
|
|
|
if self.is_unchanged() { None } else { Some(self.capture()) }
|
2017-12-17 15:21:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// The following functions build up changes
|
|
|
|
pub fn set(&mut self, key: &OsStr, value: &OsStr) {
|
2021-08-08 13:23:08 +00:00
|
|
|
let key = EnvKey::from(key);
|
2018-03-19 22:40:09 +00:00
|
|
|
self.maybe_saw_path(&key);
|
2021-08-08 13:23:08 +00:00
|
|
|
self.vars.insert(key, Some(value.to_owned()));
|
2017-12-17 15:21:47 +00:00
|
|
|
}
|
2019-09-04 02:32:44 +00:00
|
|
|
|
2017-12-17 15:21:47 +00:00
|
|
|
pub fn remove(&mut self, key: &OsStr) {
|
2021-08-08 13:23:08 +00:00
|
|
|
let key = EnvKey::from(key);
|
2018-03-19 22:40:09 +00:00
|
|
|
self.maybe_saw_path(&key);
|
2017-12-17 15:21:47 +00:00
|
|
|
if self.clear {
|
2021-08-08 13:23:08 +00:00
|
|
|
self.vars.remove(&key);
|
2017-12-17 15:21:47 +00:00
|
|
|
} else {
|
2021-08-08 13:23:08 +00:00
|
|
|
self.vars.insert(key, None);
|
2017-12-17 15:21:47 +00:00
|
|
|
}
|
|
|
|
}
|
2019-09-04 02:32:44 +00:00
|
|
|
|
2017-12-17 15:21:47 +00:00
|
|
|
pub fn clear(&mut self) {
|
|
|
|
self.clear = true;
|
|
|
|
self.vars.clear();
|
|
|
|
}
|
2019-09-04 02:32:44 +00:00
|
|
|
|
2018-03-19 22:40:09 +00:00
|
|
|
pub fn have_changed_path(&self) -> bool {
|
|
|
|
self.saw_path || self.clear
|
|
|
|
}
|
2019-09-04 02:32:44 +00:00
|
|
|
|
2021-08-08 13:23:08 +00:00
|
|
|
fn maybe_saw_path(&mut self, key: &EnvKey) {
|
2018-03-19 23:15:26 +00:00
|
|
|
if !self.saw_path && key == "PATH" {
|
2018-03-19 22:40:09 +00:00
|
|
|
self.saw_path = true;
|
|
|
|
}
|
|
|
|
}
|
2020-09-21 18:32:06 +00:00
|
|
|
|
|
|
|
pub fn iter(&self) -> CommandEnvs<'_> {
|
|
|
|
let iter = self.vars.iter();
|
|
|
|
CommandEnvs { iter }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// An iterator over the command environment variables.
|
|
|
|
///
|
|
|
|
/// This struct is created by
|
|
|
|
/// [`Command::get_envs`][crate::process::Command::get_envs]. See its
|
|
|
|
/// documentation for more.
|
2021-10-31 03:37:32 +00:00
|
|
|
#[must_use = "iterators are lazy and do nothing unless consumed"]
|
2021-10-05 22:09:11 +00:00
|
|
|
#[stable(feature = "command_access", since = "1.57.0")]
|
2020-09-21 18:32:06 +00:00
|
|
|
#[derive(Debug)]
|
|
|
|
pub struct CommandEnvs<'a> {
|
|
|
|
iter: crate::collections::btree_map::Iter<'a, EnvKey, Option<OsString>>,
|
|
|
|
}
|
|
|
|
|
2021-10-05 22:09:11 +00:00
|
|
|
#[stable(feature = "command_access", since = "1.57.0")]
|
2020-09-21 18:32:06 +00:00
|
|
|
impl<'a> Iterator for CommandEnvs<'a> {
|
|
|
|
type Item = (&'a OsStr, Option<&'a OsStr>);
|
|
|
|
fn next(&mut self) -> Option<Self::Item> {
|
|
|
|
self.iter.next().map(|(key, value)| (key.as_ref(), value.as_deref()))
|
|
|
|
}
|
|
|
|
fn size_hint(&self) -> (usize, Option<usize>) {
|
|
|
|
self.iter.size_hint()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-10-05 22:09:11 +00:00
|
|
|
#[stable(feature = "command_access", since = "1.57.0")]
|
2020-09-21 18:32:06 +00:00
|
|
|
impl<'a> ExactSizeIterator for CommandEnvs<'a> {
|
|
|
|
fn len(&self) -> usize {
|
|
|
|
self.iter.len()
|
|
|
|
}
|
|
|
|
fn is_empty(&self) -> bool {
|
|
|
|
self.iter.is_empty()
|
|
|
|
}
|
2017-12-17 15:21:47 +00:00
|
|
|
}
|