1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
|
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 alias below carries the visibility.
mod loopback_backend {
use super::Config;
// `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`.
#[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<Arc<dyn Sandbox>> {
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<Vec<SandboxInfo>> {
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<Exit> {
if cmd.argv.is_empty() {
return Err(Error::invalid("sandbox command has no argv[0]"));
}
let mut argv: Vec<String> = 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<Box<dyn Read>> {
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<PathBuf> {
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::<String>()
.trim_matches('-')
.chars()
.take(64)
.collect()
}
|