use anyhow::{bail, Result};
use std::path::{Path, PathBuf};
use std::process::Command;
use std::sync::{Arc, Mutex};
use std::time::{Duration, Instant};
use std::thread;
use configparser::ini::Ini;
pub struct Config {
path: Arc<Mutex<PathBuf>>,
config: Arc<Mutex<Ini>>,
last_reload: Arc<Mutex<Instant>>,
}
impl Config {
pub fn new() -> Self {
Config {
path: Arc::new(Mutex::new(PathBuf::new())),
config: Arc::new(Mutex::new(Ini::new())),
last_reload: Arc::new(Mutex::new(Instant::now())),
}
}
pub fn set_path(&self, path: PathBuf) -> Result<()> {
*self.path.lock().unwrap() = path.clone();
if path.exists() {
self.update_config()?;
}
self.setup_watcher(&path)?;
Ok(())
}
fn setup_watcher(&self, path: &Path) -> Result<()> {
let config_clone = Arc::clone(&self.config);
let path_clone = path.to_path_buf();
let last_reload_clone = Arc::clone(&self.last_reload);
let parent = match path.parent() {
Some(p) => p.to_path_buf(),
None => return Ok(()),
};
thread::spawn(move || {
unsafe {
let fd = libc::inotify_init1(libc::IN_CLOEXEC | libc::IN_NONBLOCK);
if fd < 0 {
eprintln!("inotify_init1 failed");
return;
}
let parent_str = match parent.to_str() {
Some(s) => s.to_string(),
None => { libc::close(fd); return; }
};
let cpath = std::ffi::CString::new(parent_str).unwrap();
let wd = libc::inotify_add_watch(
fd,
cpath.as_ptr(),
libc::IN_CREATE | libc::IN_MODIFY | libc::IN_DELETE | libc::IN_MOVED_TO,
);
if wd < 0 {
eprintln!("inotify_add_watch failed");
libc::close(fd);
return;
}
let buf_size = std::mem::size_of::<libc::inotify_event>() + 256 + 1;
let mut buf = vec![0u8; buf_size * 16];
loop {
let mut pfd = libc::pollfd {
fd,
events: libc::POLLIN,
revents: 0,
};
let ret = libc::poll(&mut pfd, 1, 1000); if ret <= 0 {
continue;
}
let n = libc::read(fd, buf.as_mut_ptr() as *mut libc::c_void, buf.len());
if n <= 0 {
continue;
}
let mut offset = 0isize;
while offset < n {
let event = &*(buf.as_ptr().offset(offset) as *const libc::inotify_event);
let name_ptr = buf.as_ptr().offset(offset + std::mem::size_of::<libc::inotify_event>() as isize);
let name_len = (0..event.len as usize).take_while(|&i| *name_ptr.add(i) != 0).count();
let name = std::str::from_utf8(std::slice::from_raw_parts(name_ptr, name_len))
.unwrap_or("");
if path_clone.file_name().and_then(|n| n.to_str()) == Some(name) {
let should_reload = {
let mut last = last_reload_clone.lock().unwrap();
let now = Instant::now();
if now.duration_since(*last) > Duration::from_millis(500) {
*last = now;
true
} else {
false
}
};
if should_reload {
let mut new_config = Ini::new();
if let Some(path_str) = path_clone.to_str() {
if new_config.load(path_str).is_ok() {
if let Ok(mut config) = config_clone.lock() {
*config = new_config;
}
}
}
}
}
offset += (std::mem::size_of::<libc::inotify_event>() + event.len as usize) as isize;
}
}
}
});
Ok(())
}
pub fn has_config(&self) -> bool {
self.path.lock().unwrap().exists()
}
pub fn get_path(&self) -> PathBuf {
self.path.lock().unwrap().clone()
}
pub fn update_config(&self) -> Result<()> {
let path = self.path.lock().unwrap().clone();
let mut new_config = Ini::new();
match new_config.load(path.to_str().unwrap_or("")) {
Ok(_) => {
*self.config.lock().unwrap() = new_config;
Ok(())
}
Err(e) => {
eprintln!(
"The following error occurred while parsing the config file:\n{}",
e
);
Ok(())
}
}
}
pub fn get_string(&self, section: &str, key: &str) -> Result<Option<String>> {
let config = self.config.lock().unwrap();
Ok(config.get(section, key))
}
pub fn get_bool(&self, section: &str, key: &str) -> Result<bool> {
let value = self.get_string(section, key)?;
match value.as_deref() {
Some("true") | Some("True") | Some("1") | Some("yes") | Some("Yes") => Ok(true),
Some("false") | Some("False") | Some("0") | Some("no") | Some("No") => Ok(false),
Some(v) => bail!("Invalid boolean value: {}", v),
None => Ok(false),
}
}
pub fn get_int(&self, section: &str, key: &str) -> Result<Option<i32>> {
let value = self.get_string(section, key)?;
match value {
Some(s) => match s.parse() {
Ok(v) => Ok(Some(v)),
Err(e) => bail!("Failed to parse integer: {}", e),
},
None => Ok(None),
}
}
pub fn get_threshold(&self, mode: &str) -> Result<u8> {
let key = match mode {
"start" => "charging_start_threshold",
"stop" => "charging_stop_threshold",
_ => bail!("Invalid threshold mode: {}", mode),
};
let value = self.get_int("battery", key)?;
match value {
Some(v) if (0..=100).contains(&v) => Ok(v as u8),
Some(v) => bail!("Threshold value out of range (0-100): {}", v),
None => Ok(if mode == "start" { 0 } else { 100 }),
}
}
pub fn has_option(&self, section: &str, key: &str) -> bool {
self.config.lock().unwrap().get(section, key).is_some()
}
pub fn get(&self, section: &str, key: &str, fallback: &str) -> String {
self.get_string(section, key)
.ok()
.flatten()
.unwrap_or_else(|| fallback.to_string())
}
}
impl Default for Config {
fn default() -> Self {
Self::new()
}
}
unsafe impl Send for Config {}
unsafe impl Sync for Config {}
lazy_static::lazy_static! {
pub static ref CONFIG: Config = Config::new();
}
pub fn find_config_file(args_config_file: Option<&str>) -> PathBuf {
let home = get_home_dir();
let user_config_dir = std::env::var("XDG_CONFIG_HOME")
.map(PathBuf::from)
.unwrap_or_else(|_| home.join(".config"));
let user_config_file = user_config_dir.join("auto-cpufreq/auto-cpufreq.conf");
let system_config_file = PathBuf::from("/etc/auto-cpufreq.conf");
if let Some(config_path) = args_config_file {
let path = PathBuf::from(config_path);
if path.is_file() {
return path;
} else {
eprintln!(
"Config file specified with '--config {}' not found.",
config_path
);
std::process::exit(1);
}
}
if user_config_file.is_file() {
return user_config_file;
}
system_config_file
}
fn get_home_dir() -> PathBuf {
let output = Command::new("sh")
.arg("-c")
.arg("getent passwd ${SUDO_USER:-$USER} | cut -d: -f6")
.output();
match output {
Ok(output) if output.status.success() => {
let home = String::from_utf8_lossy(&output.stdout);
PathBuf::from(home.trim())
}
_ => std::env::var("HOME")
.map(PathBuf::from)
.unwrap_or_else(|_| PathBuf::from("/root")),
}
}