use super::{ConfigOptions, Globals, LogStatus};
use anyhow::Result as AResult;
use serde::Deserialize;
pub type IDMapItem = (u64, u64, u64);
pub type IDMap = Vec<IDMapItem>;
#[derive(Deserialize, Default)]
pub(super) struct IDMaps {
uid: IDMap,
gid: IDMap,
}
impl<T: LogStatus> Globals<T, ConfigOptions> {
pub fn idmaps(&self) -> (&[IDMapItem], &[IDMapItem]) {
let idmaps = &self.cfg.idmaps;
(&idmaps.uid, &idmaps.gid)
}
fn remap_id(id: u64, idmap: &[IDMapItem], maptype: &str) -> AResult<u64> {
for (upper, lower, count) in idmap {
if (id >= *upper) && (id < (*upper + *count)) {
return Ok(id + *lower - *upper);
}
}
anyhow::bail!("Unable to find mapping for {maptype}: {id}");
}
pub fn remap_uid(&self, uid: u64) -> AResult<u64> {
let map = &self.cfg.idmaps.uid;
Self::remap_id(uid, map, "UID")
}
pub fn remap_gid(&self, gid: u64) -> AResult<u64> {
let map = &self.cfg.idmaps.gid;
Self::remap_id(gid, map, "GID")
}
}