use std::fs; use std::io::Read; use std::path::PathBuf; use std::sync::Arc; use crate::config::Config; use crate::error::{Error, Result}; use crate::sandbox::{Cmd, Exit, Sandbox, SandboxBackend, SandboxInfo, SandboxSpec}; /// The real definition. The module is private, so this `pub` /// is not a nameable path from outside; the alias below is the /// only way to reach it, and the alias is what carries the /// feature-conditional visibility. mod loopback_backend { use super::*; // `pub` fields inside a private module are not nameable from // outside; they stay readable by the impls below. pub struct LoopbackBackend { pub cfg: Config, } } /// The default backend, built by `backend_for`. A failure test builds one to inject a fault. /// /// `pub` only under `test-support`. Production reaches the type /// through its own module and never names it from outside; the /// outside reader that needs the name is a test. #[cfg(feature = "test-support")] pub use loopback_backend::LoopbackBackend; #[cfg(not(feature = "test-support"))] pub(crate) use loopback_backend::LoopbackBackend; impl LoopbackBackend { pub fn new(cfg: &Config) -> Self { Self { cfg: cfg.clone() } } } impl SandboxBackend for LoopbackBackend { fn acquire(&self, spec: &SandboxSpec) -> Result> { let blobs = blob_root(&self.cfg).join(safe_name(&spec.task)); fs::create_dir_all(&blobs)?; Ok(Arc::new(LoopbackSandbox { id: format!("loopback:{}", spec.task), blobs, cfg: self.cfg.clone(), })) } fn status(&self) -> Result> { Ok(vec![SandboxInfo { id: "loopback".into(), profile: self.cfg.build_profile.clone(), }]) } } struct LoopbackSandbox { id: String, blobs: PathBuf, cfg: Config, } impl Sandbox for LoopbackSandbox { fn id(&self) -> &str { &self.id } fn run(&self, cmd: &Cmd, on_log: &mut dyn FnMut(&[u8])) -> Result { if cmd.argv.is_empty() { return Err(Error::invalid("sandbox command has no argv[0]")); } let mut argv: Vec = Vec::with_capacity(cmd.argv.len()); for a in &cmd.argv { match a.strip_prefix("blob:") { Some(digest) => { let path = self.blob_path(digest)?; if !path.exists() { return Err(Error::fail_closed(format!( "blob {digest} was never staged in sandbox {}", self.id ))); } argv.push(path.display().to_string()); } None => argv.push(a.clone()), } } let mut c = std::process::Command::new(&argv[0]); c.args(&argv[1..]); if let Some(dir) = &cmd.cwd { c.current_dir(dir); } self.cfg.child_env(&mut c); let out = c.output()?; on_log(&out.stdout); on_log(&out.stderr); Ok(Exit { code: out.status.code(), stdout: out.stdout, stderr: out.stderr }) } fn put(&self, digest: &str, r: &mut dyn Read) -> Result<()> { let path = self.blob_path(digest)?; let mut f = fs::File::create(&path)?; std::io::copy(r, &mut f)?; Ok(()) } fn get(&self, digest: &str) -> Result> { let path = self.blob_path(digest)?; Ok(Box::new(fs::File::open(&path)?)) } fn destroy(&self) -> Result<()> { Ok(()) } } impl LoopbackSandbox { fn blob_path(&self, digest: &str) -> Result { if digest.is_empty() || digest.len() > 128 { return Err(Error::invalid(format!("blob digest {digest:?} is not usable"))); } if !digest.chars().all(|c| c.is_ascii_alphanumeric() || c == '-' || c == '_') { return Err(Error::invalid(format!("blob digest {digest:?} is not path-safe"))); } Ok(self.blobs.join(digest)) } } fn blob_root(cfg: &Config) -> PathBuf { cfg.staging_dir().join("blobs") } fn safe_name(s: &str) -> String { s.chars() .map(|c| if c.is_ascii_alphanumeric() { c.to_ascii_lowercase() } else { '-' }) .collect::() .trim_matches('-') .chars() .take(64) .collect() }