#[cfg(unix)]
use std::os::unix::fs::PermissionsExt as _;
use std::path::{Path, PathBuf};
use anyhow::Context as _;
pub struct Staging
{
pub root: tempfile::TempDir,
}
impl Staging
{
pub fn new() -> anyhow::Result<Self>
{
let dir = tempfile::tempdir()?;
Ok(Self { root: dir })
}
pub fn copy_binary(
&self,
binary_name: &str,
target_triple: &str,
debug: bool,
) -> anyhow::Result<PathBuf>
{
let build_dir = if debug { "debug" } else { "release" };
let src = Path::new("target")
.join(target_triple)
.join(build_dir)
.join(binary_name);
if !src.exists()
{
anyhow::bail!("Binary not found: {:?}", src);
}
let dest = self.root.path().join("usr/bin").join(binary_name);
std::fs::create_dir_all(dest.parent().context("Invalid path.")?)?;
std::fs::copy(&src, &dest)?;
#[cfg(unix)]
{
let mut perms = std::fs::metadata(&dest)?.permissions();
perms.set_mode(0o755);
std::fs::set_permissions(&dest, perms)?;
}
Ok(dest)
}
pub fn copy_extra_files(
&self,
files: &Vec<(String, String, String)>,
) -> anyhow::Result<()>
{
for (src, dest, perm) in files
{
let src_path = Path::new(src);
let dest_path = self.root.path().join(dest);
std::fs::create_dir_all(
dest_path.parent().context("Invalid path.")?,
)?;
std::fs::copy(src_path, &dest_path)?;
#[cfg(unix)]
{
let mut perms = std::fs::metadata(&dest_path)?.permissions();
perms.set_mode(u32::from_str_radix(perm, 8)?);
std::fs::set_permissions(&dest_path, perms)?;
}
}
Ok(())
}
}