use crate::io;
use crate::sys;
use crate::applets::get_arg;
pub fn mount(argc: i32, argv: *const *const u8) -> i32 {
if argc < 3 {
let fd = io::open(b"/proc/mounts", libc::O_RDONLY, 0);
if fd >= 0 {
let mut buf = [0u8; 4096];
loop {
let n = io::read(fd, &mut buf);
if n <= 0 { break; }
io::write_all(1, &buf[..n as usize]);
}
io::close(fd);
}
return 0;
}
let source = unsafe { get_arg(argv, argc - 2).unwrap() };
let target = unsafe { get_arg(argv, argc - 1).unwrap() };
let mut source_buf = [0u8; 4096];
let mut target_buf = [0u8; 4096];
source_buf[..source.len()].copy_from_slice(source);
target_buf[..target.len()].copy_from_slice(target);
let ret = unsafe {
libc::mount(
source_buf.as_ptr() as *const i8,
target_buf.as_ptr() as *const i8,
core::ptr::null(),
0,
core::ptr::null(),
)
};
if ret != 0 { sys::perror(b"mount"); 1 } else { 0 }
}
#[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_mount_show_mounts() {
let armybox = get_armybox_path();
if !armybox.exists() { return; }
let output = Command::new(&armybox)
.args(["mount"])
.output()
.unwrap();
assert_eq!(output.status.code(), Some(0));
let stdout = std::string::String::from_utf8_lossy(&output.stdout);
assert!(!stdout.is_empty() || true); }
#[test]
fn test_mount_invalid_source() {
let armybox = get_armybox_path();
if !armybox.exists() { return; }
let output = Command::new(&armybox)
.args(["mount", "/nonexistent/source", "/tmp"])
.output()
.unwrap();
assert_eq!(output.status.code(), Some(1));
}
}