Skip to content
Myr

Group together output from multi exec commands

Before adoption: this commit came in with the history the project adopted, so no landing records it.

So that if multiple `--exec` options are given, and the commands are run in parallel, the buffered output for related commands will be consecutive.

authored by Thayne McCombs · committed by David Peter · parent e54e352 · Browse files · 4 files+99 added−68 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

4 files+99 added−68 removed

  • other:+99 added−68 removed

4 files+99 added−68 removed

modified:src/exec/command.rs+81 added-35 removed
4 unmodified lines
56use crate::error::print_error;7use crate::exit_codes::ExitCode;89struct Outputs {10 stdout: Vec<u8>,11 stderr: Vec<u8>,12}13struct OutputBuf<'a> {14 out_perm: &'a Mutex<()>,15 outputs: Vec<Outputs>,16}1718impl<'a> OutputBuf<'a> {19 fn new(out_perm: &'a Mutex<()>) -> Self {20 Self {21 out_perm,22 outputs: Vec::new(),23 }24 }2526 fn push(&mut self, stdout: Vec<u8>, stderr: Vec<u8>) {27 self.outputs.push(Outputs { stdout, stderr });28 }2930 fn write(self) {31 // avoid taking the lock if there is nothing to do32 if self.outputs.is_empty() {33 return;34 }35 // While this lock is active, this thread will be the only thread allowed36 // to write its outputs.37 let _lock = self.out_perm.lock().unwrap();3839 let stdout = io::stdout();40 let stderr = io::stderr();4142 let mut stdout = stdout.lock();43 let mut stderr = stderr.lock();4445 for output in self.outputs.iter() {46 let _ = stdout.write_all(&output.stdout);47 let _ = stderr.write_all(&output.stderr);48 }49 }50}5152/// Executes a command.10pub fn execute_command(11 mut cmd: Command,53pub fn execute_commands<I: Iterator<Item = Command>>(54 cmds: I,55 out_perm: &Mutex<()>,56 enable_output_buffering: bool,57) -> ExitCode {58 let mut out_buf = OutputBuf::new(out_perm);59 for mut cmd in cmds {15 // Spawn the supplied command.16 let output = if enable_output_buffering {17 cmd.output()18 } else {19 // If running on only one thread, don't buffer output20 // Allows for viewing and interacting with intermediate command output21 cmd.spawn().and_then(|c| c.wait_with_output())22 };60 // Spawn the supplied command.61 let output = if enable_output_buffering {62 cmd.output()63 } else {64 // If running on only one thread, don't buffer output65 // Allows for viewing and interacting with intermediate command output66 cmd.spawn().and_then(|c| c.wait_with_output())67 };6824 // Then wait for the command to exit, if it was spawned.25 match output {26 Ok(output) => {27 // While this lock is active, this thread will be the only thread allowed28 // to write its outputs.29 let _lock = out_perm.lock().unwrap();3031 let stdout = io::stdout();32 let stderr = io::stderr();3334 let _ = stdout.lock().write_all(&output.stdout);35 let _ = stderr.lock().write_all(&output.stderr);3637 if output.status.code() == Some(0) {38 ExitCode::Success39 } else {40 ExitCode::GeneralError69 // Then wait for the command to exit, if it was spawned.70 match output {71 Ok(output) => {72 if enable_output_buffering {73 out_buf.push(output.stdout, output.stderr);74 }75 if output.status.code() != Some(0) {76 out_buf.write();77 return ExitCode::GeneralError;78 }79 }80 Err(why) => {81 out_buf.write();82 return handle_cmd_error(&cmd, why);83 }84 }43 Err(ref why) if why.kind() == io::ErrorKind::NotFound => {44 print_error(format!("Command not found: {:?}", cmd));45 ExitCode::GeneralError46 }47 Err(why) => {48 print_error(format!("Problem while executing command: {}", why));49 ExitCode::GeneralError50 }85 }86 out_buf.write();87 ExitCode::Success88}8990pub fn handle_cmd_error(cmd: &Command, err: io::Error) -> ExitCode {91 if err.kind() == io::ErrorKind::NotFound {92 print_error(format!("Command not found: {:?}", cmd));93 ExitCode::GeneralError94 } else {95 print_error(format!("Problem while executing command: {}", err));96 ExitCode::GeneralError97 }98}
modified:src/exec/job.rs+2 added-3 removed
47 unmodified lines
48pub fn batch(49 rx: Receiver<WorkerResult>,50 cmd: &CommandSet,51 show_filesystem_errors: bool,52 buffer_output: bool,52 limit: usize,53) -> ExitCode {54 let paths = rx.iter().filter_map(|value| match value {55 WorkerResult::Entry(path) => Some(path),
5 unmodified lines
61 }62 });63 if limit == 0 {64 // no limit66 return cmd.execute_batch(paths, buffer_output);65 return cmd.execute_batch(paths);66 }6768 let mut exit_codes = Vec::new();69 let mut peekable = paths.peekable();70 while peekable.peek().is_some() {71 let limited = peekable.by_ref().take(limit);73 let exit_code = cmd.execute_batch(limited, buffer_output);72 let exit_code = cmd.execute_batch(limited);73 exit_codes.push(exit_code);74 }75 merge_exitcodes(exit_codes)76}
modified:src/exec/mod.rs+15 added-23 removed
13 unmodified lines
14use regex::Regex;1516use crate::exit_codes::ExitCode;1718use self::command::execute_command;18use self::command::{execute_commands, handle_cmd_error};19use self::input::{basename, dirname, remove_extension};20pub use self::job::{batch, job};21use self::token::Token;22
62 unmodified lines
85 mut out_perm: Arc<Mutex<()>>,86 buffer_output: bool,87 ) -> ExitCode {88 let path_separator = self.path_separator.as_deref();89 for cmd in &self.commands {90 let exit =91 cmd.generate_and_execute(input, path_separator, &mut out_perm, buffer_output);92 if exit != ExitCode::Success {93 return exit;94 }95 }96 ExitCode::Success89 let commands = self90 .commands91 .iter()92 .map(|c| c.generate(input, path_separator));93 execute_commands(commands, &mut out_perm, buffer_output)94 }9599 pub fn execute_batch<I>(&self, paths: I, buffer_output: bool) -> ExitCode96 pub fn execute_batch<I>(&self, paths: I) -> ExitCode97 where98 I: Iterator<Item = PathBuf>,99 {100 let path_separator = self.path_separator.as_deref();101 let mut paths = paths.collect::<Vec<_>>();102 paths.sort();103 for cmd in &self.commands {107 let exit = cmd.generate_and_execute_batch(&paths, path_separator, buffer_output);104 let exit = cmd.generate_and_execute_batch(&paths, path_separator);105 if exit != ExitCode::Success {106 return exit;107 }108 }
76 unmodified lines
185186 /// Generates and executes a command.187 ///188 /// Using the internal `args` field, and a supplied `input` variable, a `Command` will be192 /// build. Once all arguments have been processed, the command is executed.193 fn generate_and_execute(194 &self,195 input: &Path,196 path_separator: Option<&str>,197 out_perm: &mut Arc<Mutex<()>>,198 buffer_output: bool,199 ) -> ExitCode {189 /// build.190 fn generate(&self, input: &Path, path_separator: Option<&str>) -> Command {191 let mut cmd = Command::new(self.args[0].generate(&input, path_separator));192 for arg in &self.args[1..] {193 cmd.arg(arg.generate(&input, path_separator));194 }204205 execute_command(cmd, out_perm, buffer_output)195 cmd196 }197198 fn generate_and_execute_batch(199 &self,200 paths: &[PathBuf],201 path_separator: Option<&str>,212 buffer_output: bool,202 ) -> ExitCode {203 let mut cmd = Command::new(self.args[0].generate("", None));204 cmd.stdin(Stdio::inherit());205 cmd.stdout(Stdio::inherit());
12 unmodified lines
218 }219 }220221 if has_path {233 execute_command(cmd, &Mutex::new(()), buffer_output)222 match cmd.spawn().and_then(|mut c| c.wait()) {223 Ok(_) => ExitCode::Success,224 Err(e) => handle_cmd_error(&cmd, e),225 }226 } else {227 ExitCode::Success228 }229 }
282 unmodified lines
modified:src/walk.rs+1 added-7 removed
348 unmodified lines
349 thread::spawn(move || {350 // This will be set to `Some` if the `--exec` argument was supplied.351 if let Some(ref cmd) = config.command {352 if cmd.in_batch_mode() {353 exec::batch(354 rx,355 cmd,356 show_filesystem_errors,357 enable_output_buffering,358 config.batch_size,359 )353 exec::batch(rx, cmd, show_filesystem_errors, config.batch_size)354 } else {355 let shared_rx = Arc::new(Mutex::new(rx));356357 let out_perm = Arc::new(Mutex::new(()));
262 unmodified lines