use crate::io;
use crate::sys;
use crate::applets::{get_arg, has_opt};
pub fn rmdir(argc: i32, argv: *const *const u8) -> i32 {
let mut parents = false;
let mut exit_code = 0;
for i in 1..argc {
if let Some(arg) = unsafe { get_arg(argv, i) } {
if arg.len() > 0 && arg[0] == b'-' {
if has_opt(arg, b'p') { parents = true; }
} else {
let result = if parents {
rmdir_parents(arg)
} else {
if io::rmdir(arg) < 0 {
sys::perror(arg);
1
} else {
0
}
};
if result != 0 {
exit_code = 1;
}
}
}
}
exit_code
}
fn rmdir_parents(path: &[u8]) -> i32 {
let mut current_path = [0u8; 4096];
let mut len = path.len().min(current_path.len() - 1);
for (i, &c) in path.iter().enumerate() {
if i < current_path.len() - 1 {
current_path[i] = c;
}
}
if io::rmdir(¤t_path[..len]) < 0 {
sys::perror(¤t_path[..len]);
return 1;
}
loop {
while len > 0 && current_path[len - 1] != b'/' {
len -= 1;
}
while len > 0 && current_path[len - 1] == b'/' {
len -= 1;
}
if len == 0 {
break;
}
if io::rmdir(¤t_path[..len]) < 0 {
break;
}
}
0
}
#[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;
fn get_armybox_path() -> PathBuf {
if let Ok(path) = std::env::var("ARMYBOX_PATH") {
return PathBuf::from(path);
}
let release = PathBuf::from("target/release/armybox");
if release.exists() { return release; }
PathBuf::from("target/debug/armybox")
}
fn setup() -> PathBuf {
let counter = TEST_COUNTER.fetch_add(1, Ordering::SeqCst);
let dir = std::env::temp_dir().join(format!("armybox_rmdir_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_rmdir_empty_directory() {
let armybox = get_armybox_path();
if !armybox.exists() { return; }
let dir = setup();
let empty_dir = dir.join("empty");
fs::create_dir(&empty_dir).unwrap();
let output = Command::new(&armybox)
.args(["rmdir", empty_dir.to_str().unwrap()])
.output()
.unwrap();
assert_eq!(output.status.code(), Some(0));
assert!(!empty_dir.exists());
cleanup(&dir);
}
#[test]
fn test_rmdir_multiple_directories() {
let armybox = get_armybox_path();
if !armybox.exists() { return; }
let dir = setup();
let dir1 = dir.join("dir1");
let dir2 = dir.join("dir2");
fs::create_dir(&dir1).unwrap();
fs::create_dir(&dir2).unwrap();
let output = Command::new(&armybox)
.args(["rmdir", dir1.to_str().unwrap(), dir2.to_str().unwrap()])
.output()
.unwrap();
assert_eq!(output.status.code(), Some(0));
assert!(!dir1.exists());
assert!(!dir2.exists());
cleanup(&dir);
}
#[test]
fn test_rmdir_nonempty_directory() {
let armybox = get_armybox_path();
if !armybox.exists() { return; }
let dir = setup();
let nonempty = dir.join("nonempty");
fs::create_dir(&nonempty).unwrap();
fs::write(nonempty.join("file.txt"), "content").unwrap();
let output = Command::new(&armybox)
.args(["rmdir", nonempty.to_str().unwrap()])
.output()
.unwrap();
assert_ne!(output.status.code(), Some(0));
assert!(nonempty.exists());
cleanup(&dir);
}
#[test]
fn test_rmdir_nonexistent() {
let armybox = get_armybox_path();
if !armybox.exists() { return; }
let output = Command::new(&armybox)
.args(["rmdir", "/nonexistent/directory"])
.output()
.unwrap();
assert_ne!(output.status.code(), Some(0));
}
#[test]
fn test_rmdir_parents() {
let armybox = get_armybox_path();
if !armybox.exists() { return; }
let dir = setup();
let nested = dir.join("a/b/c");
fs::create_dir_all(&nested).unwrap();
let output = Command::new(&armybox)
.args(["rmdir", "-p", nested.to_str().unwrap()])
.output()
.unwrap();
assert_eq!(output.status.code(), Some(0));
assert!(!nested.exists());
assert!(!dir.join("a/b").exists());
assert!(!dir.join("a").exists());
cleanup(&dir);
}
#[test]
fn test_rmdir_parents_partial() {
let armybox = get_armybox_path();
if !armybox.exists() { return; }
let dir = setup();
let nested = dir.join("a/b/c");
fs::create_dir_all(&nested).unwrap();
fs::write(dir.join("a/file.txt"), "content").unwrap();
let output = Command::new(&armybox)
.args(["rmdir", "-p", nested.to_str().unwrap()])
.output()
.unwrap();
assert_eq!(output.status.code(), Some(0));
assert!(!nested.exists());
assert!(!dir.join("a/b").exists());
assert!(dir.join("a").exists());
cleanup(&dir);
}
#[test]
fn test_rmdir_file_not_directory() {
let armybox = get_armybox_path();
if !armybox.exists() { return; }
let dir = setup();
let file = dir.join("file.txt");
fs::write(&file, "content").unwrap();
let output = Command::new(&armybox)
.args(["rmdir", file.to_str().unwrap()])
.output()
.unwrap();
assert_ne!(output.status.code(), Some(0));
assert!(file.exists());
cleanup(&dir);
}
}