|
14 | 14 | #![allow(dead_code)] |
15 | 15 |
|
16 | 16 | use std::env; |
17 | | -use std::ffi::OsString; |
| 17 | +use std::ffi::{OsStr, OsString}; |
18 | 18 | use std::fs::OpenOptions; |
19 | 19 | use std::io::Write; |
20 | | -use std::process::Command; |
| 20 | +use std::path::Path; |
| 21 | +use std::process::{Command, CommandEnvs}; |
21 | 22 | use std::str::FromStr; |
22 | 23 |
|
| 24 | +use tempfile::NamedTempFile; |
| 25 | + |
23 | 26 | /// Returns the environment variable which the dynamic library lookup path |
24 | 27 | /// resides in for this platform. |
25 | 28 | pub fn dylib_path_var() -> &'static str { |
@@ -126,3 +129,111 @@ pub fn parse_value_from_args<'a>(args: &'a [OsString], key: &str) -> Option<&'a |
126 | 129 |
|
127 | 130 | None |
128 | 131 | } |
| 132 | + |
| 133 | +/// A wrapper around [`Command`] that adds support for arg files. |
| 134 | +/// This is useful as we have some commands that can get very long and at times |
| 135 | +/// hit the OS limit (usually Windows) |
| 136 | +/// |
| 137 | +/// This implementation is based off the of `ProcessBuilder` implementation in Cargo |
| 138 | +/// but simplified. |
| 139 | +/// |
| 140 | +/// NOTE: In most scenarios we want to avoid arg files as it makes debugging more complicated |
| 141 | +/// so we try to avoid it if the command is not close the the OS limit. |
| 142 | +#[derive(Debug)] |
| 143 | +pub struct ArgFileCommand { |
| 144 | + command: Command, |
| 145 | + args: Vec<OsString>, |
| 146 | +} |
| 147 | + |
| 148 | +impl ArgFileCommand { |
| 149 | + #[track_caller] |
| 150 | + pub fn new<S: AsRef<OsStr>>(program: S) -> Self { |
| 151 | + let command = Command::new(program); |
| 152 | + Self { command, args: Vec::new() } |
| 153 | + } |
| 154 | + pub fn arg<S: AsRef<OsStr>>(&mut self, arg: S) -> &mut Self { |
| 155 | + self.args.push(arg.as_ref().to_os_string()); |
| 156 | + self |
| 157 | + } |
| 158 | + |
| 159 | + pub fn args<I, S>(&mut self, args: I) -> &mut Self |
| 160 | + where |
| 161 | + I: IntoIterator<Item = S>, |
| 162 | + S: AsRef<OsStr>, |
| 163 | + { |
| 164 | + self.args.extend(args.into_iter().map(|s| s.as_ref().to_os_string())); |
| 165 | + self |
| 166 | + } |
| 167 | + |
| 168 | + pub fn env<K, V>(&mut self, key: K, val: V) -> &mut Self |
| 169 | + where |
| 170 | + K: AsRef<OsStr>, |
| 171 | + V: AsRef<OsStr>, |
| 172 | + { |
| 173 | + self.command.env(key, val); |
| 174 | + self |
| 175 | + } |
| 176 | + |
| 177 | + pub fn get_envs(&self) -> CommandEnvs<'_> { |
| 178 | + self.command.get_envs() |
| 179 | + } |
| 180 | + |
| 181 | + pub fn env_remove<K: AsRef<OsStr>>(&mut self, key: K) -> &mut Self { |
| 182 | + self.command.env_remove(key); |
| 183 | + self |
| 184 | + } |
| 185 | + |
| 186 | + pub fn current_dir<P: AsRef<Path>>(&mut self, dir: P) -> &mut Self { |
| 187 | + self.command.current_dir(dir); |
| 188 | + self |
| 189 | + } |
| 190 | + |
| 191 | + pub fn stdin(&mut self, stdin: std::process::Stdio) -> &mut Self { |
| 192 | + self.command.stdin(stdin); |
| 193 | + self |
| 194 | + } |
| 195 | + |
| 196 | + pub fn build(mut self) -> std::io::Result<(Command, NamedTempFile)> { |
| 197 | + let mut tmp = tempfile::Builder::new().prefix("bootstrap-argfile.").tempfile()?; |
| 198 | + |
| 199 | + // On Windows there is a hard limit of ~32KB, so we cut off at 30KB to |
| 200 | + // give some buffer just incase. |
| 201 | + #[cfg(windows)] |
| 202 | + let threshold: usize = 30 * 1024; |
| 203 | + // On unix the limit is defined by ARG_MAX. If its not explicitly set we set it to 1MB |
| 204 | + // which is fairly large but lower than the ~2MB that it defaults to on most systems. |
| 205 | + #[cfg(unix)] |
| 206 | + let threshold: usize = |
| 207 | + std::env::var("ARG_MAX").ok().and_then(|v| v.parse().ok()).unwrap_or(1024 * 1024); |
| 208 | + |
| 209 | + let total_arg_len: usize = self.args.iter().map(|a| a.len() + 1).sum(); |
| 210 | + if total_arg_len <= threshold { |
| 211 | + self.command.args(self.args); |
| 212 | + return Ok((self.command, tmp)); |
| 213 | + } |
| 214 | + |
| 215 | + let mut arg = OsString::from("@"); |
| 216 | + arg.push(tmp.path()); |
| 217 | + self.command.arg(arg); |
| 218 | + |
| 219 | + let cap = self.args.iter().map(|arg| arg.len() + 1).sum::<usize>(); |
| 220 | + let mut buf = Vec::with_capacity(cap); |
| 221 | + for arg in &self.args { |
| 222 | + let arg = arg.to_str().ok_or_else(|| { |
| 223 | + std::io::Error::other(format!( |
| 224 | + "argument for argfile contains invalid UTF-8 characters: `{}`", |
| 225 | + arg.to_string_lossy() |
| 226 | + )) |
| 227 | + })?; |
| 228 | + if arg.contains('\n') { |
| 229 | + return Err(std::io::Error::other(format!( |
| 230 | + "argument for argfile contains newlines: `{arg}`" |
| 231 | + ))); |
| 232 | + } |
| 233 | + writeln!(buf, "{arg}")?; |
| 234 | + } |
| 235 | + tmp.write_all(&buf)?; |
| 236 | + |
| 237 | + Ok((self.command, tmp)) |
| 238 | + } |
| 239 | +} |
0 commit comments