use std::io::Write;
pub const BUSYBOX_BYTES: &[u8] = include_bytes!(concat!(env!("OUT_DIR"), "/busybox"));
#[cfg(feature = "wprof")]
pub const WPROF_BYTES: &[u8] = include_bytes!(concat!(env!("OUT_DIR"), "/wprof"));
fn extract_to_content_addressed_file(
bytes: &[u8],
name_hint: &str,
) -> std::io::Result<std::path::PathBuf> {
use sha2::{Digest, Sha256};
use std::os::unix::fs::PermissionsExt;
let mut hasher = Sha256::new();
hasher.update(bytes);
let digest = hasher.finalize();
let hash_hex = hex::encode(&digest[..8]);
let target = std::env::temp_dir().join(format!("ktstr-blob-{name_hint}-{hash_hex}"));
if let Ok(meta) = std::fs::metadata(&target)
&& meta.len() == bytes.len() as u64
{
return Ok(target);
}
let dir = target.parent().unwrap_or_else(|| std::path::Path::new("."));
let mut staging = tempfile::Builder::new()
.prefix(&format!("ktstr-blob-{name_hint}-staging-"))
.tempfile_in(dir)?;
staging.write_all(bytes)?;
staging.flush()?;
std::fs::set_permissions(staging.path(), std::fs::Permissions::from_mode(0o755))?;
let (_file, staging_path) = staging
.keep()
.map_err(|e| std::io::Error::other(format!("persist staging tempfile: {e}")))?;
std::fs::rename(&staging_path, &target)?;
Ok(target)
}
pub fn install_env() -> std::io::Result<()> {
if !BUSYBOX_BYTES.is_empty() {
let busybox_path = extract_to_content_addressed_file(BUSYBOX_BYTES, "busybox")?;
unsafe {
std::env::set_var(ktstr::KTSTR_BUSYBOX_PATH_ENV, &busybox_path);
}
}
#[cfg(feature = "wprof")]
{
let wprof_path = extract_to_content_addressed_file(WPROF_BYTES, "wprof")?;
unsafe {
std::env::set_var(ktstr::KTSTR_WPROF_PATH_ENV, &wprof_path);
}
}
Ok(())
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn extract_to_content_addressed_file_is_idempotent_for_same_bytes() {
let bytes = b"ktstr-blob-test-idempotent-payload";
let name = "idempotent-test";
let first = extract_to_content_addressed_file(bytes, name).expect("first extract");
let first_mtime = std::fs::metadata(&first)
.expect("first metadata")
.modified()
.expect("mtime");
let second = extract_to_content_addressed_file(bytes, name).expect("second extract");
let second_mtime = std::fs::metadata(&second)
.expect("second metadata")
.modified()
.expect("mtime");
assert_eq!(
first, second,
"same bytes must resolve to the same content-addressed path",
);
assert_eq!(
first_mtime, second_mtime,
"second call must reuse the existing file, not rewrite it (would bump mtime)",
);
let _ = std::fs::remove_file(&first);
}
#[test]
fn extract_to_content_addressed_file_distinguishes_distinct_bytes() {
let name = "distinguish-test";
let alpha =
extract_to_content_addressed_file(b"alpha-payload-bytes", name).expect("alpha extract");
let beta =
extract_to_content_addressed_file(b"beta-payload-bytes", name).expect("beta extract");
assert_ne!(
alpha, beta,
"distinct bytes under the same name_hint must produce distinct paths",
);
let _ = std::fs::remove_file(&alpha);
let _ = std::fs::remove_file(&beta);
}
#[test]
fn extract_to_content_addressed_file_sets_executable_mode() {
use std::os::unix::fs::PermissionsExt;
let path = extract_to_content_addressed_file(b"exec-mode-test-bytes", "exec-mode-test")
.expect("extract");
let mode = std::fs::metadata(&path)
.expect("metadata")
.permissions()
.mode()
& 0o777;
assert_eq!(
mode, 0o755,
"extracted blob must be rwx for owner / rx for everyone — got {mode:o}",
);
let _ = std::fs::remove_file(&path);
}
}