use std::path::{Path, PathBuf};
use heel::Grant;
#[cfg(all(unix, feature = "jobserver"))]
use crate::terminal::job_fifo_grant;
use crate::terminal::{local_path_grants, prepare_session_runtime_environment};
use crate::{PreparedTools, TerminalError};
#[derive(Debug, Clone)]
pub struct SessionSandboxPolicy {
grants: Vec<Grant>,
environment: Vec<(String, String)>,
}
impl SessionSandboxPolicy {
pub async fn new(
working_dir: &Path,
writable: &[PathBuf],
readable: &[PathBuf],
tools: &PreparedTools<PathBuf>,
) -> Result<Self, TerminalError> {
let grants = local_path_grants(writable, readable, tools)
.map_err(|error| TerminalError::SandboxSetup(error.to_string()))?;
let environment = prepare_session_runtime_environment(working_dir, tools).await?;
Ok(Self {
grants,
environment,
})
}
#[cfg(all(unix, feature = "jobserver"))]
pub fn with_job_fifo(
mut self,
fifo: &crate::jobserver::CommandFifo,
) -> Result<Self, TerminalError> {
self.grants.push(
job_fifo_grant(fifo.path())
.map_err(|error| TerminalError::SandboxSetup(error.to_string()))?,
);
self.environment.extend(fifo.environment());
Ok(self)
}
#[must_use]
pub fn grants(&self) -> &[Grant] {
&self.grants
}
#[must_use]
pub fn environment(&self) -> &[(String, String)] {
&self.environment
}
#[must_use]
pub fn into_parts(self) -> (Vec<Grant>, Vec<(String, String)>) {
(self.grants, self.environment)
}
}