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::fmt; |
8 | use crate::io; |
9 | use crate::sys::pipe::read2; |
10 | use crate::sys::process::{EnvKey, ExitStatus, Process, StdioPipes}; |
11 | |
12 | // Stores a set of changes to an environment |
13 | #[derive (Clone)] |
14 | pub struct CommandEnv { |
15 | clear: bool, |
16 | saw_path: bool, |
17 | vars: BTreeMap<EnvKey, Option<OsString>>, |
18 | } |
19 | |
20 | impl Default for CommandEnv { |
21 | fn default() -> Self { |
22 | CommandEnv { clear: false, saw_path: false, vars: Default::default() } |
23 | } |
24 | } |
25 | |
26 | impl fmt::Debug for CommandEnv { |
27 | fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { |
28 | let mut debug_command_env: DebugStruct<'_, '_> = f.debug_struct(name:"CommandEnv" ); |
29 | debug_command_env.field("clear" , &self.clear).field(name:"vars" , &self.vars); |
30 | debug_command_env.finish() |
31 | } |
32 | } |
33 | |
34 | impl CommandEnv { |
35 | // Capture the current environment with these changes applied |
36 | pub fn capture(&self) -> BTreeMap<EnvKey, OsString> { |
37 | let mut result = BTreeMap::<EnvKey, OsString>::new(); |
38 | if !self.clear { |
39 | for (k, v) in env::vars_os() { |
40 | result.insert(k.into(), v); |
41 | } |
42 | } |
43 | for (k, maybe_v) in &self.vars { |
44 | if let &Some(ref v) = maybe_v { |
45 | result.insert(k.clone(), v.clone()); |
46 | } else { |
47 | result.remove(k); |
48 | } |
49 | } |
50 | result |
51 | } |
52 | |
53 | pub fn is_unchanged(&self) -> bool { |
54 | !self.clear && self.vars.is_empty() |
55 | } |
56 | |
57 | pub fn capture_if_changed(&self) -> Option<BTreeMap<EnvKey, OsString>> { |
58 | if self.is_unchanged() { None } else { Some(self.capture()) } |
59 | } |
60 | |
61 | // The following functions build up changes |
62 | pub fn set(&mut self, key: &OsStr, value: &OsStr) { |
63 | let key = EnvKey::from(key); |
64 | self.maybe_saw_path(&key); |
65 | self.vars.insert(key, Some(value.to_owned())); |
66 | } |
67 | |
68 | pub fn remove(&mut self, key: &OsStr) { |
69 | let key = EnvKey::from(key); |
70 | self.maybe_saw_path(&key); |
71 | if self.clear { |
72 | self.vars.remove(&key); |
73 | } else { |
74 | self.vars.insert(key, None); |
75 | } |
76 | } |
77 | |
78 | pub fn clear(&mut self) { |
79 | self.clear = true; |
80 | self.vars.clear(); |
81 | } |
82 | |
83 | pub fn does_clear(&self) -> bool { |
84 | self.clear |
85 | } |
86 | |
87 | pub fn have_changed_path(&self) -> bool { |
88 | self.saw_path || self.clear |
89 | } |
90 | |
91 | fn maybe_saw_path(&mut self, key: &EnvKey) { |
92 | if !self.saw_path && key == "PATH" { |
93 | self.saw_path = true; |
94 | } |
95 | } |
96 | |
97 | pub fn iter(&self) -> CommandEnvs<'_> { |
98 | let iter = self.vars.iter(); |
99 | CommandEnvs { iter } |
100 | } |
101 | } |
102 | |
103 | /// An iterator over the command environment variables. |
104 | /// |
105 | /// This struct is created by |
106 | /// [`Command::get_envs`][crate::process::Command::get_envs]. See its |
107 | /// documentation for more. |
108 | #[must_use = "iterators are lazy and do nothing unless consumed" ] |
109 | #[stable (feature = "command_access" , since = "1.57.0" )] |
110 | #[derive (Debug)] |
111 | pub struct CommandEnvs<'a> { |
112 | iter: crate::collections::btree_map::Iter<'a, EnvKey, Option<OsString>>, |
113 | } |
114 | |
115 | #[stable (feature = "command_access" , since = "1.57.0" )] |
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: &OsString, value: &Option)| (key.as_ref(), value.as_deref())) |
120 | } |
121 | fn size_hint(&self) -> (usize, Option<usize>) { |
122 | self.iter.size_hint() |
123 | } |
124 | } |
125 | |
126 | #[stable (feature = "command_access" , since = "1.57.0" )] |
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 | } |
135 | |
136 | pub fn wait_with_output( |
137 | mut process: Process, |
138 | mut pipes: StdioPipes, |
139 | ) -> io::Result<(ExitStatus, Vec<u8>, Vec<u8>)> { |
140 | drop(pipes.stdin.take()); |
141 | |
142 | let (mut stdout: Vec, mut stderr: Vec) = (Vec::new(), Vec::new()); |
143 | match (pipes.stdout.take(), pipes.stderr.take()) { |
144 | (None, None) => {} |
145 | (Some(out: AnonPipe), None) => { |
146 | let res: Result = out.read_to_end(&mut stdout); |
147 | res.unwrap(); |
148 | } |
149 | (None, Some(err: AnonPipe)) => { |
150 | let res: Result = err.read_to_end(&mut stderr); |
151 | res.unwrap(); |
152 | } |
153 | (Some(out: AnonPipe), Some(err: AnonPipe)) => { |
154 | let res: Result<(), Error> = read2(p1:out, &mut stdout, p2:err, &mut stderr); |
155 | res.unwrap(); |
156 | } |
157 | } |
158 | |
159 | let status: ExitStatus = process.wait()?; |
160 | Ok((status, stdout, stderr)) |
161 | } |
162 | |