Unify execute_command across platforms
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by Matthias Reitinger · committed by David Peter · parent 51aea57 · Browse files · 1 file·+3 added−73 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
1 file+3 added−73 removed
- other:+3 added−73 removed
Provenance
- Commit
5784823- Tree
67e0a87- Author
Matthias Reitinger <reima@edenprime.de>- Committer
David Peter <sharkdp@users.noreply.github.com>- Parent
51aea57
1 file changed+3 added−73 removed
modified:src/exec/command.rs+3 added-73 removed
7 unmodified lines8 9 use std::process::Command;10 use std::sync::{Arc, Mutex};11 use std::io;12+use std::io::Write;13 14 /// Executes a command.14-#[cfg(not(unix))]15 pub fn execute_command(mut cmd: Command, out_perm: Arc<Mutex<()>>) {16- use std::process::Stdio;17- use std::io::Write;18-16 // Spawn the supplied command.20- let output = cmd.stdout(Stdio::piped()).stderr(Stdio::piped()).output();17+ let output = cmd.output();18 19 // Then wait for the command to exit, if it was spawned.20 match output {21 Ok(output) => {22 // While this lock is active, this thread will be the only thread allowed26- // to write it's outputs.23+ // to write its outputs.24 let _lock = out_perm.lock().unwrap();25 26 let stdout = io::stdout();27 let stderr = io::stderr();9 unmodified lines37 }38 }39 }40 }44-45-/// Executes a command.46-#[cfg(all(unix))]47-pub fn execute_command(mut cmd: Command, out_perm: Arc<Mutex<()>>) {48- use libc::{close, dup2, pipe, STDERR_FILENO, STDOUT_FILENO};49- use std::fs::File;50- use std::os::unix::process::CommandExt;51- use std::os::unix::io::FromRawFd;52-53- // Initial a pair of pipes that will be used to54- // pipe the std{out,err} of the spawned process.55- let mut stdout_fds = [0; 2];56- let mut stderr_fds = [0; 2];57-58- unsafe {59- pipe(stdout_fds.as_mut_ptr());60- pipe(stderr_fds.as_mut_ptr());61- }62-63- // Configure the pipes accordingly in the child.64- let child = cmd.before_exec(move || unsafe {65- // Redirect the child's std{out,err} to the write ends of our pipe.66- dup2(stdout_fds[1], STDOUT_FILENO);67- dup2(stderr_fds[1], STDERR_FILENO);68-69- // Close all the fds we created here, so EOF will be sent when the program exits.70- close(stdout_fds[0]);71- close(stdout_fds[1]);72- close(stderr_fds[0]);73- close(stderr_fds[1]);74- Ok(())75- }).spawn();76-77- // Open the read end of the pipes as `File`s.78- let (mut pout, mut perr) = unsafe {79- // Close the write ends of the pipes in the parent80- close(stdout_fds[1]);81- close(stderr_fds[1]);82- (83- // But create files from the read ends.84- File::from_raw_fd(stdout_fds[0]),85- File::from_raw_fd(stderr_fds[0]),86- )87- };88-89- match child {90- Ok(mut child) => {91- let _ = child.wait();92-93- // Create a lock to ensure that this thread has exclusive access to writing.94- let _lock = out_perm.lock().unwrap();95-96- // And then write the outputs of the process until EOF is sent to each file.97- let stdout = io::stdout();98- let stderr = io::stderr();99- let _ = io::copy(&mut pout, &mut stdout.lock());100- let _ = io::copy(&mut perr, &mut stderr.lock());101- }102- Err(why) => {103- if why.kind() == io::ErrorKind::NotFound {104- eprintln!("fd: execution error: command not found");105- } else {106- eprintln!("fd: execution error: {}", why);107- }108- }109- }110-}