]> git.proxmox.com Git - rustc.git/blob - library/std/src/sys_common/process.rs
New upstream version 1.55.0+dfsg1
[rustc.git] / library / std / src / sys_common / process.rs
1 #![allow(dead_code)]
2 #![unstable(feature = "process_internals", issue = "none")]
3
4 use crate::collections::BTreeMap;
5 use crate::env;
6 use crate::ffi::{OsStr, OsString};
7 use crate::sys::process::EnvKey;
8
9 // Stores a set of changes to an environment
10 #[derive(Clone, Debug)]
11 pub struct CommandEnv {
12 clear: bool,
13 saw_path: bool,
14 vars: BTreeMap<EnvKey, Option<OsString>>,
15 }
16
17 impl Default for CommandEnv {
18 fn default() -> Self {
19 CommandEnv { clear: false, saw_path: false, vars: Default::default() }
20 }
21 }
22
23 impl CommandEnv {
24 // Capture the current environment with these changes applied
25 pub fn capture(&self) -> BTreeMap<EnvKey, OsString> {
26 let mut result = BTreeMap::<EnvKey, OsString>::new();
27 if !self.clear {
28 for (k, v) in env::vars_os() {
29 result.insert(k.into(), v);
30 }
31 }
32 for (k, maybe_v) in &self.vars {
33 if let &Some(ref v) = maybe_v {
34 result.insert(k.clone(), v.clone());
35 } else {
36 result.remove(k);
37 }
38 }
39 result
40 }
41
42 // Apply these changes directly to the current environment
43 pub fn apply(&self) {
44 if self.clear {
45 for (k, _) in env::vars_os() {
46 env::remove_var(k);
47 }
48 }
49 for (key, maybe_val) in self.vars.iter() {
50 if let Some(ref val) = maybe_val {
51 env::set_var(key, val);
52 } else {
53 env::remove_var(key);
54 }
55 }
56 }
57
58 pub fn is_unchanged(&self) -> bool {
59 !self.clear && self.vars.is_empty()
60 }
61
62 pub fn capture_if_changed(&self) -> Option<BTreeMap<EnvKey, OsString>> {
63 if self.is_unchanged() { None } else { Some(self.capture()) }
64 }
65
66 // The following functions build up changes
67 pub fn set(&mut self, key: &OsStr, value: &OsStr) {
68 let key = EnvKey::from(key);
69 self.maybe_saw_path(&key);
70 self.vars.insert(key, Some(value.to_owned()));
71 }
72
73 pub fn remove(&mut self, key: &OsStr) {
74 let key = EnvKey::from(key);
75 self.maybe_saw_path(&key);
76 if self.clear {
77 self.vars.remove(&key);
78 } else {
79 self.vars.insert(key, None);
80 }
81 }
82
83 pub fn clear(&mut self) {
84 self.clear = true;
85 self.vars.clear();
86 }
87
88 pub fn have_changed_path(&self) -> bool {
89 self.saw_path || self.clear
90 }
91
92 fn maybe_saw_path(&mut self, key: &EnvKey) {
93 if !self.saw_path && key == "PATH" {
94 self.saw_path = true;
95 }
96 }
97
98 pub fn iter(&self) -> CommandEnvs<'_> {
99 let iter = self.vars.iter();
100 CommandEnvs { iter }
101 }
102 }
103
104 /// An iterator over the command environment variables.
105 ///
106 /// This struct is created by
107 /// [`Command::get_envs`][crate::process::Command::get_envs]. See its
108 /// documentation for more.
109 #[unstable(feature = "command_access", issue = "44434")]
110 #[derive(Debug)]
111 pub struct CommandEnvs<'a> {
112 iter: crate::collections::btree_map::Iter<'a, EnvKey, Option<OsString>>,
113 }
114
115 #[unstable(feature = "command_access", issue = "44434")]
116 impl<'a> Iterator for CommandEnvs<'a> {
117 type Item = (&'a OsStr, Option<&'a OsStr>);
118 fn next(&mut self) -> Option<Self::Item> {
119 self.iter.next().map(|(key, value)| (key.as_ref(), value.as_deref()))
120 }
121 fn size_hint(&self) -> (usize, Option<usize>) {
122 self.iter.size_hint()
123 }
124 }
125
126 #[unstable(feature = "command_access", issue = "44434")]
127 impl<'a> ExactSizeIterator for CommandEnvs<'a> {
128 fn len(&self) -> usize {
129 self.iter.len()
130 }
131 fn is_empty(&self) -> bool {
132 self.iter.is_empty()
133 }
134 }