"Fossies" - the Fresh Open Source Software Archive

Member "fd-8.1.1/src/exec/command.rs" (25 May 2020, 1294 Bytes) of package /linux/privat/fd-8.1.1.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) Rust source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file. See also the last Fossies "Diffs" side-by-side code changes report for "command.rs": 7.5.0_vs_8.0.0.

    1 use std::io;
    2 use std::io::Write;
    3 use std::process::Command;
    4 use std::sync::Mutex;
    5 
    6 use crate::error::print_error;
    7 use crate::exit_codes::ExitCode;
    8 
    9 /// Executes a command.
   10 pub fn execute_command(mut cmd: Command, out_perm: &Mutex<()>) -> ExitCode {
   11     // Spawn the supplied command.
   12     let output = cmd.output();
   13 
   14     // Then wait for the command to exit, if it was spawned.
   15     match output {
   16         Ok(output) => {
   17             // While this lock is active, this thread will be the only thread allowed
   18             // to write its outputs.
   19             let _lock = out_perm.lock().unwrap();
   20 
   21             let stdout = io::stdout();
   22             let stderr = io::stderr();
   23 
   24             let _ = stdout.lock().write_all(&output.stdout);
   25             let _ = stderr.lock().write_all(&output.stderr);
   26 
   27             if output.status.code() == Some(0) {
   28                 ExitCode::Success
   29             } else {
   30                 ExitCode::GeneralError
   31             }
   32         }
   33         Err(ref why) if why.kind() == io::ErrorKind::NotFound => {
   34             print_error(format!("Command not found: {:?}", cmd));
   35             ExitCode::GeneralError
   36         }
   37         Err(why) => {
   38             print_error(format!("Problem while executing command: {}", why));
   39             ExitCode::GeneralError
   40         }
   41     }
   42 }