fix: add binary check on config reload

This commit is contained in:
Vladimir Rubin 2025-05-03 13:59:44 +03:00
parent 3ab9006bdc
commit ea14f03e1f
Signed by: vavakado
GPG key ID: CAB744727F36B524

View file

@ -219,7 +219,13 @@ async fn main() {
Config::default()
};
// TODO: add a map to not check for binaries twice?
fn check_binary(binary: &str) {
if !is_binary_available(binary) {
error!("{binary} is not available, exiting.");
std::process::exit(1);
}
}
match cfg.gamma_backend {
GammaBackend::Hyprctl => check_binary("hyprctl"),
GammaBackend::Ddcutil => check_binary("ddcutil"),
@ -341,9 +347,39 @@ async fn reload_config(notify: Arc<Notify>) {
match Config::load(&config_path) {
Ok(cfg) => {
debug!("Config file reloaded successfully");
fn check_binary(binary: &str) -> bool {
if !is_binary_available(binary) {
error!("{binary} is not available, exiting.");
return false;
}
true
}
let gamma_check = match cfg.gamma_backend {
GammaBackend::Hyprctl => check_binary("hyprctl"),
GammaBackend::Ddcutil => check_binary("ddcutil"),
GammaBackend::Xsct => check_binary("xsct"),
GammaBackend::Redshift => check_binary("redshift"),
GammaBackend::Gammastep => check_binary("gammastep"),
GammaBackend::None => true,
};
let temp_check = match cfg.temp_backend {
TempBackend::Hyprctl => check_binary("hyprctl"),
TempBackend::Gammastep => check_binary("gammastep"),
TempBackend::Xsct => check_binary("xsct"),
TempBackend::Redshift => check_binary("redshift"),
TempBackend::None => true,
};
if !gamma_check || !temp_check {
error!("One or more binaries are not available, retaining old config.");
return;
}
*config = cfg;
reset_cache();
trace!("new config: {:#?}", config);
let new_modified = std::fs::metadata(&config_path)
.and_then(|m| m.modified())
@ -359,6 +395,7 @@ async fn reload_config(notify: Arc<Notify>) {
}
}
}
fn is_binary_available(binary_name: &str) -> bool {
use std::fs;
if let Ok(paths) = env::var("PATH") {
@ -411,10 +448,3 @@ fn config_handle() -> Arc<RwLock<Config>> {
fn get_time() -> Time {
Time::now().expect("Failed to get local time")
}
fn check_binary(binary: &str) {
if !is_binary_available(binary) {
error!("{binary} is not available, exiting.");
std::process::exit(1);
}
}