command.rs
This file
Path
src/exec/command.rs
110 lines · 3.6 KiB · Rust
Last changed by
Update error message if command could not be foundsharkdpbefore adoption5ed7514
History of this fileTake it
Remove CommandTicket to avoid Vec allocationMatthias Reitingerbefore adoptionc18caaa
lines 1–34why, one step back1// Copyright (c) 2017 fd developers2// Licensed under the Apache License, Version 2.03// <LICENSE-APACHE or http://www.apache.org/licenses/LICENSE-2.0>4// or the MIT license <LICENSE-MIT or http://opensource.org/licenses/MIT>,5// at your option. All files in the project carrying such6// notice may not be copied, modified, or distributed except7// according to those terms.89use std::process::Command;10use std::sync::{Arc, Mutex};11use std::io;1213/// Executes a command.14#[cfg(not(unix))]15pub fn execute_command(mut cmd: Command, out_perm: Arc<Mutex<()>>) {16 use std::process::Stdio;17 use std::io::Write;1819 // Spawn the supplied command.20 let output = cmd.stdout(Stdio::piped()).stderr(Stdio::piped()).output();2122 // Then wait for the command to exit, if it was spawned.23 match output {24 Ok(output) => {25 // While this lock is active, this thread will be the only thread allowed26 // to write it's outputs.27 let _lock = out_perm.lock().unwrap();2829 let stdout = io::stdout();30 let stderr = io::stderr();3132 let _ = stdout.lock().write_all(&output.stdout);33 let _ = stderr.lock().write_all(&output.stderr);34 }Update error message if command could not be foundsharkdpbefore adoption5ed7514
lines 35–41why, one step back35 Err(why) => {36 if why.kind() == io::ErrorKind::NotFound {37 eprintln!("fd: execution error: command not found");38 } else {39 eprintln!("fd: execution error: {}", why);40 }41 }Remove CommandTicket to avoid Vec allocationMatthias Reitingerbefore adoptionc18caaa
lines 42–93why, one step back42 }43}4445/// Executes a command.46#[cfg(all(unix))]47pub 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;5253 // 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];5758 unsafe {59 pipe(stdout_fds.as_mut_ptr());60 pipe(stderr_fds.as_mut_ptr());61 }6263 // 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);6869 // 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();7677 // 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 };8889 match child {90 Ok(mut child) => {91 let _ = child.wait();9293 // Create a lock to ensure that this thread has exclusive access to writing.Fix execute_command on UnixMatthias Reitingerbefore adoptionccdbc76
line 94why, one step back94 let _lock = out_perm.lock().unwrap();Remove CommandTicket to avoid Vec allocationMatthias Reitingerbefore adoptionc18caaa
lines 95–101why, one step back9596 // 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 }Update error message if command could not be foundsharkdpbefore adoption5ed7514
lines 102–108why, one step back102 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 }Remove CommandTicket to avoid Vec allocationMatthias Reitingerbefore adoptionc18caaa
lines 109–110why, one step back109 }110}