Skip to main content

cargo_mate/sweeping/src/
embedder.rs

1use anyhow::Result;
2use std::fs;
3use std::path::Path;
4#[cfg(feature = "embedded_binary")]
5pub const EMBEDDED_SWEEP_BINARY: &[u8] = include_bytes!(env!("SWEEP_BINARY_PATH"));
6#[cfg(not(feature = "embedded_binary"))]
7pub const EMBEDDED_SWEEP_BINARY: &[u8] = b"";
8pub fn extract_sweep_binary() -> Result<Vec<u8>> {
9    use super::encryption::decrypt_binary;
10    let encrypted_base64 = String::from_utf8(EMBEDDED_SWEEP_BINARY.to_vec())?;
11    decrypt_binary(&encrypted_base64)
12}
13pub fn write_sweep_binary_to_temp() -> Result<std::path::PathBuf> {
14    use tempfile::NamedTempFile;
15    let binary_data = extract_sweep_binary()?;
16    let temp_file = NamedTempFile::new()?;
17    fs::write(&temp_file, binary_data)?;
18    Ok(temp_file.path().to_path_buf())
19}
20pub fn execute_sweep_binary(args: &[&str]) -> Result<std::process::Output> {
21    use std::process::Command;
22    let temp_path = write_sweep_binary_to_temp()?;
23    #[cfg(unix)]
24    {
25        use std::os::unix::fs::PermissionsExt;
26        let mut perms = fs::metadata(&temp_path)?.permissions();
27        perms.set_mode(0o755);
28        fs::set_permissions(&temp_path, perms)?;
29    }
30    let output = Command::new(&temp_path).args(args).output()?;
31    let _ = fs::remove_file(temp_path);
32    Ok(output)
33}
34#[cfg(test)]
35mod tests {
36    use super::*;
37    #[test]
38    fn test_binary_extraction() {
39        if !EMBEDDED_SWEEP_BINARY.is_empty() {
40            let result = extract_sweep_binary();
41            assert!(result.is_ok());
42        }
43    }
44}