2017-11-15 01:56:32 +01:00
|
|
|
// Copyright (c) 2017 fd developers
|
|
|
|
// Licensed under the Apache License, Version 2.0
|
|
|
|
// <LICENSE-APACHE or http://www.apache.org/licenses/LICENSE-2.0>
|
|
|
|
// or the MIT license <LICENSE-MIT or http://opensource.org/licenses/MIT>,
|
|
|
|
// at your option. All files in the project carrying such
|
|
|
|
// notice may not be copied, modified, or distributed except
|
|
|
|
// according to those terms.
|
|
|
|
|
|
|
|
use std::io;
|
2017-12-09 02:46:08 +01:00
|
|
|
use std::io::Write;
|
2018-04-13 22:46:17 +02:00
|
|
|
use std::process::Command;
|
|
|
|
use std::sync::{Arc, Mutex};
|
2017-11-15 01:56:32 +01:00
|
|
|
|
|
|
|
/// Executes a command.
|
|
|
|
pub fn execute_command(mut cmd: Command, out_perm: Arc<Mutex<()>>) {
|
|
|
|
// Spawn the supplied command.
|
2017-12-09 02:46:08 +01:00
|
|
|
let output = cmd.output();
|
2017-11-15 01:56:32 +01:00
|
|
|
|
|
|
|
// Then wait for the command to exit, if it was spawned.
|
|
|
|
match output {
|
|
|
|
Ok(output) => {
|
|
|
|
// While this lock is active, this thread will be the only thread allowed
|
2017-12-09 02:46:08 +01:00
|
|
|
// to write its outputs.
|
2017-11-15 01:56:32 +01:00
|
|
|
let _lock = out_perm.lock().unwrap();
|
|
|
|
|
|
|
|
let stdout = io::stdout();
|
|
|
|
let stderr = io::stderr();
|
|
|
|
|
|
|
|
let _ = stdout.lock().write_all(&output.stdout);
|
|
|
|
let _ = stderr.lock().write_all(&output.stderr);
|
|
|
|
}
|
2018-10-03 16:48:38 +02:00
|
|
|
Err(ref why) if why.kind() == io::ErrorKind::NotFound => {
|
2018-10-03 15:53:59 +02:00
|
|
|
print_error!("fd: execution error: command not found");
|
2018-10-03 16:48:38 +02:00
|
|
|
}
|
2017-11-15 23:24:11 +01:00
|
|
|
Err(why) => {
|
2018-10-03 15:53:59 +02:00
|
|
|
print_error!("fd: execution error: {}", why);
|
2017-11-15 23:24:11 +01:00
|
|
|
}
|
2017-11-15 01:56:32 +01:00
|
|
|
}
|
|
|
|
}
|