2023-03-20 19:55:45 +00:00
|
|
|
// This file is part of the uutils coreutils package.
|
|
|
|
//
|
|
|
|
// For the full copyright and license information, please view the LICENSE
|
|
|
|
// file that was distributed with this source code.
|
|
|
|
|
2023-11-08 22:06:11 +00:00
|
|
|
use libc::{close, dup, dup2, pipe, STDERR_FILENO, STDOUT_FILENO};
|
2023-09-28 06:42:30 +00:00
|
|
|
use std::ffi::OsString;
|
2023-09-28 19:48:34 +00:00
|
|
|
use std::io;
|
2023-11-08 11:46:24 +00:00
|
|
|
use std::io::Write;
|
2023-11-08 22:06:11 +00:00
|
|
|
use std::os::fd::RawFd;
|
2023-03-20 19:55:45 +00:00
|
|
|
use std::process::Command;
|
|
|
|
use std::sync::atomic::Ordering;
|
|
|
|
use std::sync::{atomic::AtomicBool, Once};
|
|
|
|
|
|
|
|
static CHECK_GNU: Once = Once::new();
|
|
|
|
static IS_GNU: AtomicBool = AtomicBool::new(false);
|
|
|
|
|
|
|
|
pub fn is_gnu_cmd(cmd_path: &str) -> Result<(), std::io::Error> {
|
|
|
|
CHECK_GNU.call_once(|| {
|
|
|
|
let version_output = Command::new(cmd_path).arg("--version").output().unwrap();
|
|
|
|
|
|
|
|
println!("version_output {:#?}", version_output);
|
|
|
|
|
|
|
|
let version_str = String::from_utf8_lossy(&version_output.stdout).to_string();
|
|
|
|
if version_str.contains("GNU coreutils") {
|
|
|
|
IS_GNU.store(true, Ordering::Relaxed);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
if IS_GNU.load(Ordering::Relaxed) {
|
|
|
|
Ok(())
|
|
|
|
} else {
|
|
|
|
panic!("Not the GNU implementation");
|
|
|
|
}
|
|
|
|
}
|
2023-09-28 06:42:30 +00:00
|
|
|
|
2023-11-08 22:06:11 +00:00
|
|
|
pub fn generate_and_run_uumain<F>(args: &[OsString], uumain_function: F) -> (String, String, i32)
|
2023-09-28 06:42:30 +00:00
|
|
|
where
|
|
|
|
F: FnOnce(std::vec::IntoIter<OsString>) -> i32,
|
|
|
|
{
|
|
|
|
let uumain_exit_status;
|
|
|
|
|
2023-11-08 22:06:11 +00:00
|
|
|
// Duplicate the stdout and stderr file descriptors
|
2023-09-28 06:42:30 +00:00
|
|
|
let original_stdout_fd = unsafe { dup(STDOUT_FILENO) };
|
2023-11-08 22:06:11 +00:00
|
|
|
let original_stderr_fd = unsafe { dup(STDERR_FILENO) };
|
|
|
|
if original_stdout_fd == -1 || original_stderr_fd == -1 {
|
|
|
|
return (
|
|
|
|
"Failed to duplicate STDOUT_FILENO or STDERR_FILENO".to_string(),
|
|
|
|
"".to_string(),
|
|
|
|
-1,
|
|
|
|
);
|
2023-11-08 11:46:24 +00:00
|
|
|
}
|
|
|
|
println!("Running test {:?}", &args[0..]);
|
2023-11-08 22:06:11 +00:00
|
|
|
let mut pipe_stdout_fds = [-1; 2];
|
|
|
|
let mut pipe_stderr_fds = [-1; 2];
|
|
|
|
|
|
|
|
// Create pipes for stdout and stderr
|
|
|
|
if unsafe { pipe(pipe_stdout_fds.as_mut_ptr()) } == -1
|
|
|
|
|| unsafe { pipe(pipe_stderr_fds.as_mut_ptr()) } == -1
|
|
|
|
{
|
|
|
|
return ("Failed to create pipes".to_string(), "".to_string(), -1);
|
2023-11-08 11:46:24 +00:00
|
|
|
}
|
|
|
|
|
2023-11-08 22:06:11 +00:00
|
|
|
// Redirect stdout and stderr to their respective pipes
|
|
|
|
if unsafe { dup2(pipe_stdout_fds[1], STDOUT_FILENO) } == -1
|
|
|
|
|| unsafe { dup2(pipe_stderr_fds[1], STDERR_FILENO) } == -1
|
|
|
|
{
|
|
|
|
unsafe {
|
|
|
|
close(pipe_stdout_fds[0]);
|
|
|
|
close(pipe_stdout_fds[1]);
|
|
|
|
close(pipe_stderr_fds[0]);
|
|
|
|
close(pipe_stderr_fds[1]);
|
2023-11-08 11:46:24 +00:00
|
|
|
}
|
2023-11-08 22:06:11 +00:00
|
|
|
return (
|
|
|
|
"Failed to redirect STDOUT_FILENO or STDERR_FILENO".to_string(),
|
|
|
|
"".to_string(),
|
|
|
|
-1,
|
|
|
|
);
|
2023-09-28 06:42:30 +00:00
|
|
|
}
|
2023-11-08 11:46:24 +00:00
|
|
|
|
|
|
|
uumain_exit_status = uumain_function(args.to_owned().into_iter());
|
2023-11-08 11:46:46 +00:00
|
|
|
io::stdout().flush().unwrap();
|
2023-11-08 22:06:11 +00:00
|
|
|
io::stderr().flush().unwrap();
|
|
|
|
|
|
|
|
// Restore the original stdout and stderr
|
|
|
|
if unsafe { dup2(original_stdout_fd, STDOUT_FILENO) } == -1
|
|
|
|
|| unsafe { dup2(original_stderr_fd, STDERR_FILENO) } == -1
|
|
|
|
{
|
|
|
|
return (
|
|
|
|
"Failed to restore the original STDOUT_FILENO or STDERR_FILENO".to_string(),
|
|
|
|
"".to_string(),
|
|
|
|
-1,
|
|
|
|
);
|
|
|
|
}
|
2023-11-08 11:46:24 +00:00
|
|
|
unsafe {
|
|
|
|
close(original_stdout_fd);
|
2023-11-08 22:06:11 +00:00
|
|
|
close(original_stderr_fd);
|
2023-11-08 11:46:24 +00:00
|
|
|
}
|
2023-11-08 22:06:11 +00:00
|
|
|
unsafe { close(pipe_stdout_fds[1]) };
|
|
|
|
unsafe { close(pipe_stderr_fds[1]) };
|
|
|
|
|
|
|
|
let captured_stdout = read_from_fd(pipe_stdout_fds[0]).trim().to_string();
|
|
|
|
let captured_stderr = read_from_fd(pipe_stderr_fds[0]).to_string();
|
|
|
|
let captured_stderr = captured_stderr
|
|
|
|
.splitn(2, ':')
|
|
|
|
.nth(1)
|
|
|
|
.unwrap_or("")
|
|
|
|
.trim()
|
|
|
|
.to_string();
|
|
|
|
|
|
|
|
(captured_stdout, captured_stderr, uumain_exit_status)
|
|
|
|
}
|
2023-09-28 06:42:30 +00:00
|
|
|
|
2023-11-08 22:06:11 +00:00
|
|
|
fn read_from_fd(fd: RawFd) -> String {
|
2023-09-28 06:42:30 +00:00
|
|
|
let mut captured_output = Vec::new();
|
|
|
|
let mut read_buffer = [0; 1024];
|
|
|
|
loop {
|
|
|
|
let bytes_read = unsafe {
|
|
|
|
libc::read(
|
2023-11-08 22:06:11 +00:00
|
|
|
fd,
|
2023-09-28 06:42:30 +00:00
|
|
|
read_buffer.as_mut_ptr() as *mut libc::c_void,
|
|
|
|
read_buffer.len(),
|
|
|
|
)
|
|
|
|
};
|
2023-11-08 11:46:24 +00:00
|
|
|
|
|
|
|
if bytes_read == -1 {
|
|
|
|
eprintln!("Failed to read from the pipe");
|
|
|
|
break;
|
|
|
|
}
|
2023-11-08 22:06:11 +00:00
|
|
|
if bytes_read == 0 {
|
2023-09-28 06:42:30 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
captured_output.extend_from_slice(&read_buffer[..bytes_read as usize]);
|
|
|
|
}
|
|
|
|
|
2023-11-08 22:06:11 +00:00
|
|
|
unsafe { libc::close(fd) };
|
2023-09-28 06:42:30 +00:00
|
|
|
|
2023-11-08 22:06:11 +00:00
|
|
|
String::from_utf8_lossy(&captured_output).into_owned()
|
2023-09-28 06:42:30 +00:00
|
|
|
}
|
2023-09-28 19:48:34 +00:00
|
|
|
|
|
|
|
pub fn run_gnu_cmd(
|
|
|
|
cmd_path: &str,
|
|
|
|
args: &[OsString],
|
|
|
|
check_gnu: bool,
|
2023-11-08 22:06:11 +00:00
|
|
|
) -> Result<(String, String, i32), (String, String, i32)> {
|
2023-09-28 19:48:34 +00:00
|
|
|
if check_gnu {
|
2023-11-08 22:06:11 +00:00
|
|
|
match is_gnu_cmd(cmd_path) {
|
|
|
|
Ok(_) => {} // if the check passes, do nothing
|
|
|
|
Err(e) => {
|
|
|
|
// Convert the io::Error into the function's error type
|
|
|
|
return Err((String::new(), e.to_string(), -1));
|
|
|
|
}
|
|
|
|
}
|
2023-09-28 19:48:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
let mut command = Command::new(cmd_path);
|
|
|
|
for arg in args {
|
|
|
|
command.arg(arg);
|
|
|
|
}
|
|
|
|
|
2023-11-08 22:06:11 +00:00
|
|
|
let output = match command.output() {
|
|
|
|
Ok(output) => output,
|
|
|
|
Err(e) => return Err((String::new(), e.to_string(), -1)),
|
|
|
|
};
|
2023-09-28 19:48:34 +00:00
|
|
|
let exit_code = output.status.code().unwrap_or(-1);
|
2023-11-08 22:06:11 +00:00
|
|
|
|
|
|
|
// Here we get stdout and stderr as Strings
|
|
|
|
let stdout = String::from_utf8_lossy(&output.stdout).to_string();
|
|
|
|
let stderr = String::from_utf8_lossy(&output.stderr).to_string();
|
|
|
|
let stderr = stderr
|
|
|
|
.splitn(2, ':')
|
|
|
|
.nth(1)
|
|
|
|
.unwrap_or("")
|
|
|
|
.trim()
|
|
|
|
.to_string();
|
|
|
|
|
2023-09-28 19:48:34 +00:00
|
|
|
if output.status.success() || !check_gnu {
|
2023-11-08 22:06:11 +00:00
|
|
|
Ok((stdout, stderr, exit_code))
|
2023-09-28 19:48:34 +00:00
|
|
|
} else {
|
2023-11-08 22:06:11 +00:00
|
|
|
Err((stdout, stderr, exit_code))
|
2023-09-28 19:48:34 +00:00
|
|
|
}
|
|
|
|
}
|