use crate::sys;
use crate::applets::get_arg;
pub fn mkfifo(argc: i32, argv: *const *const u8) -> i32 {
let mode = 0o644u32;
let mut exit_code = 0;
for i in 1..argc {
if let Some(path) = unsafe { get_arg(argv, i) } {
if path.len() > 0 && path[0] != b'-' {
if unsafe { libc::mkfifo(path.as_ptr() as *const i8, mode) } < 0 {
sys::perror(path);
exit_code = 1;
}
}
}
}
exit_code
}
#[cfg(test)]
mod tests {
extern crate std;
use std::sync::atomic::{AtomicUsize, Ordering};
static TEST_COUNTER: AtomicUsize = AtomicUsize::new(0);
use std::process::Command;
use std::fs;
use std::path::PathBuf;
use std::os::unix::fs::FileTypeExt;
fn get_armybox_path() -> PathBuf {
if let Ok(path) = std::env::var("ARMYBOX_PATH") {
return PathBuf::from(path);
}
let manifest_dir = std::env::var("CARGO_MANIFEST_DIR")
.map(PathBuf::from)
.unwrap_or_else(|_| std::env::current_dir().unwrap());
let release = manifest_dir.join("target/release/armybox");
if release.exists() { return release; }
manifest_dir.join("target/debug/armybox")
}
fn setup() -> PathBuf {
let counter = TEST_COUNTER.fetch_add(1, Ordering::SeqCst);
let dir = std::env::temp_dir().join(format!("armybox_mkfifo_test_{}_{}", std::process::id(), counter));
let _ = fs::remove_dir_all(&dir);
fs::create_dir_all(&dir).unwrap();
dir
}
fn cleanup(dir: &std::path::Path) {
let _ = fs::remove_dir_all(dir);
}
#[test]
fn test_mkfifo_basic() {
let armybox = get_armybox_path();
if !armybox.exists() { return; }
let dir = setup();
let fifo = dir.join("myfifo");
let output = Command::new(&armybox)
.args(["mkfifo", fifo.to_str().unwrap()])
.output()
.unwrap();
assert_eq!(output.status.code(), Some(0));
assert!(fifo.exists());
let metadata = fs::metadata(&fifo).unwrap();
assert!(metadata.file_type().is_fifo());
cleanup(&dir);
}
#[test]
fn test_mkfifo_multiple() {
let armybox = get_armybox_path();
if !armybox.exists() { return; }
let dir = setup();
let fifo1 = dir.join("fifo1");
let fifo2 = dir.join("fifo2");
let output = Command::new(&armybox)
.args(["mkfifo", fifo1.to_str().unwrap(), fifo2.to_str().unwrap()])
.output()
.unwrap();
assert_eq!(output.status.code(), Some(0));
assert!(fs::metadata(&fifo1).unwrap().file_type().is_fifo());
assert!(fs::metadata(&fifo2).unwrap().file_type().is_fifo());
cleanup(&dir);
}
#[test]
fn test_mkfifo_exists() {
let armybox = get_armybox_path();
if !armybox.exists() { return; }
let dir = setup();
let fifo = dir.join("myfifo");
let fifo_cstr = std::ffi::CString::new(fifo.to_str().unwrap()).unwrap();
unsafe { libc::mkfifo(fifo_cstr.as_ptr(), 0o644) };
let output = Command::new(&armybox)
.args(["mkfifo", fifo.to_str().unwrap()])
.output()
.unwrap();
assert_ne!(output.status.code(), Some(0));
cleanup(&dir);
}
#[test]
fn test_mkfifo_no_permission() {
let armybox = get_armybox_path();
if !armybox.exists() { return; }
if std::env::var("USER").map(|u| u == "root").unwrap_or(false) {
return; }
let output = Command::new(&armybox)
.args(["mkfifo", "/root/testfifo"])
.output()
.unwrap();
assert_ne!(output.status.code(), Some(0));
}
}