From 9942014989bcc56b1a302ef6eb2b9f2e2aecccb8 Mon Sep 17 00:00:00 2001 From: Vova Kryachko Date: Wed, 11 Jun 2025 09:26:50 -0400 Subject: [PATCH] fix(build/oci): Propagate passAsFile files into the build sandbox. fixes #87 Change-Id: Ia8109ae678bddb1072e15f6a3ed63e3e3c862b6a Reviewed-on: https://cl.snix.dev/c/snix/+/30238 Autosubmit: Vova Kryachko Reviewed-by: Florian Klink Tested-by: besadii Reviewed-by: Yureka --- snix/build/src/oci/bundle.rs | 32 ++++++++++++++++++++++++++++++-- 1 file changed, 30 insertions(+), 2 deletions(-) diff --git a/snix/build/src/oci/bundle.rs b/snix/build/src/oci/bundle.rs index 00db19acb..b5d10a440 100644 --- a/snix/build/src/oci/bundle.rs +++ b/snix/build/src/oci/bundle.rs @@ -1,7 +1,9 @@ //! Module to create an OCI runtime bundle for a given [BuildRequest]. use std::{ + ffi::OsStr, fs, - path::{Path, PathBuf}, + io::Write, + path::{Component, Path, PathBuf}, }; use super::scratch_name; @@ -39,7 +41,33 @@ pub(crate) fn make_bundle<'a>( for p in request.scratch_paths.iter() { let scratch_path = scratch_root.join(scratch_name(p)); debug!(scratch_path=?scratch_path, path=?p, "about to create scratch dir"); - fs::create_dir_all(scratch_path).context("Unable to create scratch dir")?; + fs::create_dir_all(scratch_path.clone()).context("Unable to create scratch dir")?; + + // TODO(#152): this is a hack, in the general case we may not have the "build" directory and additional files + // may not have /build prefix. But in practice today snix_build.rs is the only user of the builder and + // it always sets up a /build scratch and populates all additional_files with the /build prefix. + // For now this unblocks builds, but worth improving in the future. + if p == Path::new("build") { + for file in request.additional_files.iter() { + if file.path.components().count() < 2 + || file.path.components().next() != Some(Component::Normal(OsStr::new("build"))) + { + Err(std::io::Error::other( + "Additional files must start with build/", + ))? + } + + // remove build/ prefix + let p = file.path.components().skip(1).collect::(); + if let Some(parent) = p.parent() { + fs::create_dir_all(scratch_path.clone().join(parent)) + .context("Failed to create dir for additional file")?; + } + let p = scratch_path.join(p); + let mut out = std::fs::File::create(p).context("could not create file")?; + out.write_all(&file.contents)?; + } + } } Ok(())