use std::fs;
#[cfg(target_family = "unix")]
use std::os::unix::fs::PermissionsExt;
use std::path::{Path, PathBuf};
pub fn copy_binary(
binary_name: &str,
source_dir: &Path,
target_dir: &Path,
) -> Result<PathBuf, String> {
let source_path = source_dir.join(binary_name);
let target_path = target_dir.join(binary_name);
fs::copy(&source_path, &target_path)
.map_err(|e| format!("Failed to copy binary file '{}': {}", binary_name, e))?;
#[cfg(target_family = "unix")]
{
let permissions = fs::Permissions::from_mode(0o755);
fs::set_permissions(&target_path, permissions)
.map_err(|e| format!("Failed to set permissions for '{}': {}", binary_name, e))?;
}
Ok(target_path)
}
#[cfg(test)]
mod tests {
use super::*;
use std::fs::{self, File};
use std::io::Write;
use tempfile::TempDir;
#[test]
fn test_copy_binary() {
let temp_dir = TempDir::new().unwrap();
let source_dir = temp_dir.path();
let target_temp_dir = TempDir::new().unwrap();
let target_dir = target_temp_dir.path();
let binary_name = "test_binary";
let source_file_path = source_dir.join(binary_name);
let mut file = File::create(&source_file_path).unwrap();
writeln!(file, "binary content").unwrap();
let copy_result = copy_binary(binary_name, source_dir, &target_dir);
assert!(copy_result.is_ok());
assert!(target_dir.join(binary_name).exists());
}
}