Build and test code now takes Arc<dyn Sandbox>. Context implements the trait as a bridge for the unshare driver, so the callers move in one commit without rewriting the driver first. Staging goes through stage_tree with explicit ReuseState upper and work paths. The sources backup writes its copy explicitly, and is_host keeps the keyring cache ownership check on the host only.
149 lines
4.6 KiB
Rust
149 lines
4.6 KiB
Rust
//! The `Sandbox` bridge for the old `Context` API.
|
|
//!
|
|
//! The build code receives `Arc<dyn Sandbox>` 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<Execution> {
|
|
let driver_guard = self.driver();
|
|
let driver = driver_guard.as_ref().unwrap();
|
|
let program = cmd.program.to_string_lossy().into_owned();
|
|
let args: Vec<String> = 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<String> {
|
|
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<bool> {
|
|
Context::exists(self, path)
|
|
}
|
|
|
|
fn is_dir(&self, path: &Path) -> io::Result<bool> {
|
|
Context::is_dir(self, path)
|
|
}
|
|
|
|
fn list_files(&self, path: &Path) -> io::Result<Vec<std::path::PathBuf>> {
|
|
Context::list_files(self, path)
|
|
}
|
|
|
|
fn put(&self, host: &Path, dest: &Path, opts: &PutOptions) -> io::Result<PutOutcome> {
|
|
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<String> {
|
|
Context::create_temp_dir(self)
|
|
}
|
|
|
|
fn is_host(&self) -> bool {
|
|
matches!(self.config, crate::context::ContextConfig::Local)
|
|
}
|
|
|
|
fn cleanup(&self, _outcome: Option<BuildOutcome>) -> io::Result<()> {
|
|
Context::cleanup(self)
|
|
}
|
|
}
|