//! The `Sandbox` bridge for the old `Context` API. //! //! The build code receives `Arc` while the unshare driver is //! still a `ContextDriver`. This bridge maps the trait onto the driver until //! the driver moves into `sandbox::unshare`. use std::io; use std::path::Path; use crate::sandbox::{ BuildOutcome, Capture, CommandSpec, Execution, PutOptions, PutOutcome, Sandbox, }; use super::Context; impl Sandbox for Context { fn run(&self, cmd: &CommandSpec, capture: Capture) -> io::Result { let driver_guard = self.driver(); let driver = driver_guard.as_ref().unwrap(); let program = cmd.program.to_string_lossy().into_owned(); let args: Vec = cmd .args .iter() .map(|arg| arg.to_string_lossy().into_owned()) .collect(); let env: Vec<(String, String)> = cmd .env .iter() .map(|(key, value)| { ( key.to_string_lossy().into_owned(), value.to_string_lossy().into_owned(), ) }) .collect(); let cwd = cmd.cwd.as_deref().map(|path| path.to_string_lossy()); match capture { Capture::Inherit => { let status = driver.run(&program, &args, &env, cwd.as_deref())?; Ok(Execution { status, stdout: Vec::new(), stderr: Vec::new(), }) } Capture::Lines(sink) => { let status = driver.run_captured(&program, &args, &env, cwd.as_deref(), sink)?; Ok(Execution { status, stdout: Vec::new(), stderr: Vec::new(), }) } Capture::Output => { let output = driver.run_output(&program, &args, &env, cwd.as_deref())?; Ok(Execution { status: output.status, stdout: output.stdout, stderr: output.stderr, }) } } } fn read_file(&self, path: &Path) -> io::Result { Context::read_file(self, path) } fn write_file(&self, path: &Path, content: &str) -> io::Result<()> { Context::write_file(self, path, content) } fn exists(&self, path: &Path) -> io::Result { Context::exists(self, path) } fn is_dir(&self, path: &Path) -> io::Result { Context::is_dir(self, path) } fn list_files(&self, path: &Path) -> io::Result> { Context::list_files(self, path) } fn put(&self, host: &Path, dest: &Path, opts: &PutOptions) -> io::Result { let dest_root = dest.parent().ok_or_else(|| { io::Error::new(io::ErrorKind::InvalidInput, "destination has no parent") })?; let dest_root = dest_root.to_string_lossy(); if let Some(reuse) = &opts.reuse { let staging = Context::ensure_available_with_overlay( self, host, &dest_root, &reuse.upper, &reuse.work, )?; return Ok(if staging.overlay { PutOutcome::Resumed } else { PutOutcome::Fresh }); } let staged = Context::ensure_available(self, host, &dest_root)?; if staged != dest { return Err(io::Error::new( io::ErrorKind::InvalidInput, format!( "put destination '{}' does not match the staged path '{}'", dest.display(), staged.display() ), )); } Ok(PutOutcome::Fresh) } fn get(&self, src: &Path, dest: &Path) -> io::Result<()> { Context::retrieve_path(self, src, dest) } fn remove(&self, path: &Path) -> io::Result<()> { let status = Context::command(self, "rm").arg("-rf").arg(path).status()?; if status.success() { Ok(()) } else { Err(io::Error::other(format!( "failed to remove '{}': rm exited with {status}", path.display() ))) } } fn create_temp_dir(&self) -> io::Result { Context::create_temp_dir(self) } fn is_host(&self) -> bool { matches!(self.config, crate::context::ContextConfig::Local) } fn cleanup(&self, _outcome: Option) -> io::Result<()> { Context::cleanup(self) } }