1use log::{info};
7use std::path::Path;
8use std::process::Command;
9use crate::error::DaemonError;
10
11pub struct Binary {
13 path: String,
14}
15
16impl Binary {
17 pub fn new(path: String) -> Self {
19 Binary { path }
20 }
21
22 pub fn load(&self) -> Result<(), DaemonError> {
24 info!("Loading binary: {}", self.path);
25
26 if !Path::new(&self.path).exists() {
27 return Err(DaemonError::CustomError(format!("Binary not found: {}", self.path)));
28 }
29
30 let output = Command::new(&self.path)
32 .output()
33 .map_err(|e| DaemonError::CustomError(format!("Failed to execute binary: {}", e)))?;
34
35 if !output.status.success() {
36 return Err(DaemonError::CustomError(format!(
37 "Binary failed with output: {}",
38 String::from_utf8_lossy(&output.stderr)
39 )));
40 }
41
42 info!("Successfully loaded binary: {}", self.path);
43 Ok(())
44 }
45
46 pub fn unload(&self) -> Result<(), DaemonError> {
48 info!("Unloading binary: {}", self.path);
49
50 let output = Command::new("pkill")
52 .arg(&self.path)
53 .output()
54 .map_err(|e| DaemonError::CustomError(format!("Failed to unload binary: {}", e)))?;
55
56 if !output.status.success() {
57 return Err(DaemonError::CustomError(format!(
58 "Failed to unload binary: {}",
59 String::from_utf8_lossy(&output.stderr)
60 )));
61 }
62
63 info!("Successfully unloaded binary: {}", self.path);
64 Ok(())
65 }
66}
67
68pub struct BinaryManager {
70 binaries: Vec<Binary>,
71}
72
73impl BinaryManager {
74 pub fn new(binaries: Vec<String>) -> Self {
76 BinaryManager {
77 binaries: binaries.into_iter().map(Binary::new).collect(),
78 }
79 }
80
81 pub fn load_all(&self) -> Result<(), DaemonError> {
83 for binary in &self.binaries {
84 binary.load()?;
85 }
86 Ok(())
87 }
88
89 pub fn unload_all(&self) -> Result<(), DaemonError> {
91 for binary in &self.binaries {
92 binary.unload()?;
93 }
94 Ok(())
95 }
96}