use std::{fs::Permissions, os::unix::prelude::PermissionsExt};
use crate::npk::manifest::{mount::Mount, Manifest};
use anyhow::{Context, Result};
use log::debug;
use nix::unistd;
use tokio::fs;
use super::config;
const PERSIST_DIR_PERMISSIONS: u32 = 0o700;
pub(crate) async fn setup(config: &config::Config, manifest: &Manifest) -> Result<()> {
if manifest
.mounts
.iter()
.any(|(_, mount)| matches!(mount, Mount::Persist))
{
let dir = config.data_dir.join(manifest.name.as_ref());
if !dir.exists() {
debug!("Creating {}", dir.display());
fs::create_dir_all(&dir)
.await
.with_context(|| format!("failed to create directory {}", dir.display()))?;
}
debug!(
"Setting directory mode {} on {}",
umask::Mode::from(PERSIST_DIR_PERMISSIONS),
dir.display(),
);
fs::set_permissions(&dir, Permissions::from_mode(PERSIST_DIR_PERMISSIONS))
.await
.with_context(|| format!("failed to set permission on {}", dir.display()))?;
let uid = unistd::Uid::from_raw(manifest.uid.into());
let gid = unistd::Gid::from_raw(manifest.gid.into());
debug!("Chowning {} to {uid}:{gid}", dir.display());
unistd::chown(dir.as_os_str(), Some(uid), Some(gid)).context(format!(
"failed to chown {} to {}:{}",
dir.display(),
uid,
gid
))?;
}
Ok(())
}