use crate::io;
use crate::sys;
use super::get_arg;
const BLKRRPART: crate::io::IoctlReq = 0x125fu32 as crate::io::IoctlReq;
pub fn partprobe(argc: i32, argv: *const *const u8) -> i32 {
let mut exit_code = 0;
if argc < 2 {
let disks = [
b"/dev/sda" as &[u8],
b"/dev/sdb",
b"/dev/sdc",
b"/dev/sdd",
b"/dev/nvme0n1",
b"/dev/vda",
b"/dev/hda",
];
for disk in disks.iter() {
let fd = io::open(disk, libc::O_RDONLY, 0);
if fd >= 0 {
if unsafe { libc::ioctl(fd, BLKRRPART) } < 0 {
}
io::close(fd);
}
}
return 0;
}
for i in 1..argc {
if let Some(device) = unsafe { get_arg(argv, i) } {
let fd = io::open(device, libc::O_RDONLY, 0);
if fd < 0 {
sys::perror(device);
exit_code = 1;
continue;
}
if unsafe { libc::ioctl(fd, BLKRRPART) } < 0 {
io::write_str(2, b"partprobe: ");
io::write_all(2, device);
io::write_str(2, b": failed to reread partition table\n");
exit_code = 1;
}
io::close(fd);
}
}
exit_code
}
#[cfg(test)]
mod tests {
extern crate std;
use std::process::Command;
use std::path::PathBuf;
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")
}
#[test]
fn test_partprobe_no_args() {
let armybox = get_armybox_path();
if !armybox.exists() { return; }
let output = Command::new(&armybox)
.args(["partprobe"])
.output()
.unwrap();
assert_eq!(output.status.code(), Some(0));
}
#[test]
fn test_partprobe_nonexistent() {
let armybox = get_armybox_path();
if !armybox.exists() { return; }
let output = Command::new(&armybox)
.args(["partprobe", "/dev/nonexistent_device_xyz"])
.output()
.unwrap();
assert_eq!(output.status.code(), Some(1));
}
}