#[cfg(target_os = "linux")]
pub const BWRAP_PATH: &str = "/usr/bin/bwrap";
#[cfg(target_os = "linux")]
pub fn is_available() -> bool {
std::path::Path::new(BWRAP_PATH).exists()
}
#[cfg(not(target_os = "linux"))]
pub fn is_available() -> bool {
false
}
#[cfg(target_os = "linux")]
pub fn build_bwrap_command(cwd: &std::path::Path, program: &str, args: &[String]) -> Vec<String> {
let mut cmd: Vec<String> = Vec::with_capacity(10 + args.len());
cmd.push(BWRAP_PATH.to_string());
cmd.push("--ro-bind".to_string());
cmd.push("/".to_string());
cmd.push("/".to_string());
let cwd_str = cwd.to_string_lossy().to_string();
cmd.push("--bind".to_string());
cmd.push(cwd_str.clone());
cmd.push(cwd_str.clone());
cmd.push("--chdir".to_string());
cmd.push(cwd_str);
cmd.push("--unshare-all".to_string());
cmd.push("--".to_string());
cmd.push(program.to_string());
cmd.extend(args.iter().cloned());
cmd
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_is_available_does_not_panic() {
let _ = is_available();
}
#[test]
#[cfg(target_os = "linux")]
fn test_build_bwrap_command_structure() {
let cwd = std::path::Path::new("/home/user/project");
let cmd = build_bwrap_command(cwd, "sh", &["-c".to_string(), "echo hi".to_string()]);
assert_eq!(cmd[0], "/usr/bin/bwrap");
assert!(cmd.contains(&"--ro-bind".to_string()));
assert!(cmd.contains(&"--chdir".to_string()));
assert_eq!(cmd[cmd.len() - 1], "echo hi");
assert_eq!(cmd[cmd.len() - 2], "-c");
assert_eq!(cmd[cmd.len() - 3], "sh");
}
}