sandbox,deb,test: run builds through the Sandbox trait

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.
This commit is contained in:
vhaudiquet committed 2026-11-24 18:35:17 +01:00
1 parent f1a21e0923
commit 483e532baf
16 files changed
+362 -109

No files matched your search

+41 -30
View File
@@ -13,6 +13,7 @@ use crate::logfmt::{
MmdebstrapClassifier, QuiltClassifier,
};
use crate::report::{BuildTarget, BuildView, TargetKind};
use crate::sandbox::{PutOutcome, ReuseState, Sandbox, SandboxExt, StageOptions};
use std::error::Error;
use std::path::{Path, PathBuf};
use std::sync::Arc;
@@ -602,7 +603,7 @@ async fn build_binary_package_impl(
// never from the process-global, which concurrent builds may have
// re-pointed at their own chroot); otherwise the base context is used
// directly.
let build_ctx = match guard.as_ref() {
let build_ctx: Arc<dyn Sandbox> = match guard.as_ref() {
Some(g) => g.context(),
None => base_ctx.clone(),
};
@@ -653,7 +654,12 @@ async fn build_binary_package_impl(
}
}
}
let staged_path = if let Some(s) = &session {
let tree_name = host_tree
.file_name()
.map(PathBuf::from)
.unwrap_or_else(|| host_tree.clone());
let staged_path = Path::new(&build_root).join(tree_name);
if let Some(s) = &session {
// Re-mount the session's persistent upperdir over the host
// tree. If overlay staging is unavailable (no overlayfs, mount
// failure, non-overlay driver), wipe the staged tree and
@@ -661,13 +667,16 @@ async fn build_binary_package_impl(
// tree would merge stale build artifacts and resurrect
// host-deleted files into the build. Still environment-
// resumable, just not artifact-resumable.
let staging = match build_ctx.ensure_available_with_overlay(
&host_tree,
&build_root,
&s.upper_dir(),
&s.work_dir(),
) {
Ok(staging) => staging,
let opts = StageOptions {
reuse: Some(ReuseState {
upper: s.upper_dir(),
work: s.work_dir(),
}),
prune_vcs: true,
};
match build_ctx.stage_tree(&host_tree, &staged_path, &opts) {
Ok(PutOutcome::Resumed) => {}
Ok(PutOutcome::Fresh) => build_resume = false,
Err(e) => {
log::info!(
"Overlay staging unavailable ({}): the staged tree is \
@@ -675,23 +684,24 @@ async fn build_binary_package_impl(
e
);
s.wipe_staged_tree()?;
let path = build_ctx.ensure_available(&host_tree, &build_root)?;
let opts = StageOptions {
reuse: None,
prune_vcs: true,
};
build_ctx.stage_tree(&host_tree, &staged_path, &opts)?;
build_resume = false;
crate::context::OverlayStaging {
path,
overlay: false,
}
}
};
if !staging.overlay {
// Copy fallback: the staged tree is fresh, there is nothing
// incremental to build on.
build_resume = false;
}
staging.path
} else {
build_ctx.ensure_available(&host_tree, &build_root)?
};
build_ctx.stage_tree(
&host_tree,
&staged_path,
&StageOptions {
reuse: None,
prune_vcs: true,
},
)?;
}
// The staged tree (build root + tree name) is where the build
// steps and the artifact collection work.
let build_root = staged_path.to_string_lossy().into_owned();
@@ -760,7 +770,7 @@ async fn build_binary_package_impl(
for (idx, remote_file) in remote_files.iter().enumerate() {
let file_name = remote_file.file_name().ok_or("Invalid remote filename")?;
let local_dest = parent_dir.join(file_name);
build_ctx.retrieve_path(remote_file, &local_dest)?;
build_ctx.get(remote_file, &local_dest)?;
artifacts.push(local_dest);
view.progress("Retrieving artifacts", idx + 1, total_debs);
@@ -998,7 +1008,7 @@ pub(crate) fn resolve_package_directory(
package: &str,
version: &str,
series: &str,
ctx: &context::Context,
ctx: &Arc<dyn Sandbox>,
) -> Result<PathBuf, Box<dyn Error>> {
if ctx.is_dir(build_root)? && ctx.exists(&build_root.join("debian"))? {
log::debug!(
@@ -1019,7 +1029,7 @@ pub(crate) fn find_package_directory(
package: &str,
version: &str,
series: &str,
ctx: &context::Context,
ctx: &Arc<dyn Sandbox>,
) -> Result<PathBuf, Box<dyn Error>> {
// Check quirks first for custom package directories
let custom_dirs = crate::quirks::get_package_directories(package, series);
@@ -1128,7 +1138,7 @@ fn find_dsc_file(
build_root: &str,
package: &str,
version: &str,
ctx: &Arc<Context>,
ctx: &Arc<dyn Sandbox>,
) -> Result<PathBuf, Box<dyn Error>> {
// Strip epoch from version (e.g., "1:2.3.4-5" -> "2.3.4-5")
let version_without_epoch = version.split_once(':').map(|(_, v)| v).unwrap_or(version);
@@ -1269,15 +1279,15 @@ mod tests {
/// context like the ephemeral build contexts are: exists/list_files/
/// is_dir answer through the path mapping, no namespace privileges
/// needed.
fn unshare_test_context(chroot_root: &Path) -> Context {
fn unshare_test_context(chroot_root: &Path) -> Arc<dyn Sandbox> {
let base = Context::new(crate::context::ContextConfig::Local).unwrap();
Context::with_parent(
Arc::new(Context::with_parent(
crate::context::ContextConfig::Unshare {
path: chroot_root.to_string_lossy().to_string(),
parent: None,
},
Arc::new(base),
)
))
}
/// The staging-area listing must classify entries through the context:
@@ -1400,6 +1410,7 @@ mod tests {
// Create a fresh local context for this test
let ctx = Arc::new(Context::new(crate::context::ContextConfig::Local).unwrap());
let ctx_sandbox: Arc<dyn Sandbox> = ctx.clone();
// Change directory to the package directory
let cwd = crate::deb::find_package_directory(
@@ -1407,7 +1418,7 @@ mod tests {
package,
&package_info.stanza.version,
series,
&ctx,
&ctx_sandbox,
)
.expect("Cannot find package directory");
log::debug!("Package directory: {}", cwd.display());