use crate::{error::*, file::BptConf, io::ProcessCredentials};
use nix::unistd::{Uid, geteuid};
use std::cell::RefCell;
pub struct QueryCredentials<'a> {
bpt_conf: &'a BptConf,
euid: Uid,
credentials: RefCell<Option<Option<ProcessCredentials>>>,
}
impl<'a> QueryCredentials<'a> {
pub fn new(bpt_conf: &'a BptConf) -> Self {
Self::new_for_euid(bpt_conf, geteuid())
}
pub(crate) fn new_for_euid(bpt_conf: &'a BptConf, euid: Uid) -> Self {
Self {
bpt_conf,
euid,
credentials: RefCell::new(None),
}
}
pub fn get(&self) -> Result<Option<ProcessCredentials>, Err> {
if let Some(credentials) = self.credentials.borrow().as_ref() {
return Ok(credentials.clone());
}
let credentials = self.bpt_conf.build_credentials_for_euid(self.euid)?;
*self.credentials.borrow_mut() = Some(credentials.clone());
Ok(credentials)
}
}