use cap_std::fs::Dir;
use cap_std::io_lifetimes;
use cap_tempfile::cap_std;
use io_lifetimes::OwnedFd;
use rustix::fd::{AsFd, FromRawFd, IntoRawFd};
use std::os::unix::process::CommandExt;
use std::sync::Arc;
pub trait CapStdExtCommandExt {
fn take_fd_n(&mut self, fd: Arc<OwnedFd>, target: i32) -> &mut Self;
fn cwd_dir(&mut self, dir: Dir) -> &mut Self;
}
#[allow(unsafe_code)]
impl CapStdExtCommandExt for std::process::Command {
fn take_fd_n(&mut self, fd: Arc<OwnedFd>, target: i32) -> &mut Self {
unsafe {
self.pre_exec(move || {
let mut target = OwnedFd::from_raw_fd(target);
rustix::io::dup2(&*fd, &mut target)?;
let _ = target.into_raw_fd();
Ok(())
});
}
self
}
fn cwd_dir(&mut self, dir: Dir) -> &mut Self {
unsafe {
self.pre_exec(move || {
rustix::process::fchdir(dir.as_fd())?;
Ok(())
});
}
self
}
}