use std::fs;
use std::path::Path;
use crate::constants::{CHROOTS, CRON, KEYS, MAILBOX, SQLITE};
use crate::error::{Error, Result};
use crate::scope::validate_chroot_name;
pub fn init_chroot(global_root: &Path, name: &str) -> Result<()> {
validate_chroot_name(name)?;
let base = global_root.join(CHROOTS).join(name);
if base.exists() {
return Err(Error::msg(format!("chroot already exists: {name}")));
}
for sub in [KEYS, MAILBOX, CRON, SQLITE] {
fs::create_dir_all(base.join(sub))?;
}
Ok(())
}
pub fn list_chroots(global_root: &Path) -> Result<Vec<String>> {
let dir = global_root.join(CHROOTS);
if !dir.is_dir() {
return Ok(Vec::new());
}
let mut names = Vec::new();
for entry in fs::read_dir(&dir)? {
let entry = entry?;
if entry.file_type()?.is_dir() {
names.push(entry.file_name().to_string_lossy().into_owned());
}
}
names.sort();
Ok(names)
}