use std::os::unix::ffi::OsStrExt;
use std::path::{Path, PathBuf};
use std::sync::mpsc;
use std::time::Duration;
pub(crate) fn within_timeout<T: Send + 'static>(f: impl FnOnce() -> T + Send + 'static) -> T {
let (tx, rx) = mpsc::channel();
std::thread::spawn(move || {
if tx.send(f()).is_err() {
eprintln!("special-file watchdog receiver closed before the worker completed");
}
});
rx.recv_timeout(Duration::from_secs(10))
.expect("a read entry point must NOT block on a special file (missing O_NONBLOCK?)")
}
pub(crate) fn make_fifo(dir: &Path, name: &str) -> PathBuf {
let path = dir.join(name);
let c = std::ffi::CString::new(path.as_os_str().as_bytes()).unwrap();
let rc = unsafe { libc::mkfifo(c.as_ptr(), 0o600) };
assert_eq!(rc, 0, "mkfifo failed: {}", std::io::Error::last_os_error());
path
}
pub(crate) fn symlink_to(dir: &Path, link: &str, target: &Path) -> PathBuf {
let path = dir.join(link);
std::os::unix::fs::symlink(target, &path).unwrap();
path
}
pub(crate) fn write_regular(dir: &Path, name: &str, bytes: &[u8]) -> PathBuf {
let path = dir.join(name);
std::fs::write(&path, bytes).unwrap();
path
}