Skip to content

Commit

Permalink
Separated generation of base and config files
Browse files Browse the repository at this point in the history
Now the daemon generates profiles/config files manually if it does not find them.

This is done so that profile files are not overriden during pkgbuild update.
  • Loading branch information
TheAlexDev23 committed Sep 11, 2024
1 parent fc9a020 commit 818594e
Show file tree
Hide file tree
Showing 7 changed files with 88 additions and 17 deletions.
2 changes: 1 addition & 1 deletion ci/gen_daemon_pkgbuild.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ def create_pkgbuild(pkgname, pkgver, url):
install -Dm755 "target/release/power-daemon-mgr" "$pkgdir/usr/bin/power-daemon-mgr"
# Generate files
"$pkgdir/usr/bin/power-daemon-mgr" -v generate-files --path "$pkgdir" --program-path "/usr/bin/power-daemon-mgr"
"$pkgdir/usr/bin/power-daemon-mgr" -v generate-base-files --path "$pkgdir" --program-path "/usr/bin/power-daemon-mgr"
}}
post_install() {{
Expand Down
2 changes: 1 addition & 1 deletion ci/gen_daemon_pkgbuild_git.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ def create_pkgbuild(pkgname, pkgver, url):
install -Dm755 "target/release/power-daemon-mgr" "$pkgdir/usr/bin/power-daemon-mgr"
# Generate files
"$pkgdir/usr/bin/power-daemon-mgr" -v generate-files --path "$pkgdir" --program-path "/usr/bin/power-daemon-mgr"
"$pkgdir/usr/bin/power-daemon-mgr" -v generate-base-files --path "$pkgdir" --program-path "/usr/bin/power-daemon-mgr"
}}
post_install() {{
Expand Down
3 changes: 2 additions & 1 deletion crates/power-daemon-mgr/dev-tmp-install-clean.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@ cargo build

sudo rm /usr/lib/systemd/system/power-options.service
sudo cp ../../target/debug/power-daemon-mgr -f /usr/bin/
sudo power-daemon-mgr -vvv generate-files --path / --program-path /usr/bin/power-daemon-mgr --verbose-daemon
sudo power-daemon-mgr -vvv generate-base-files --path / --program-path /usr/bin/power-daemon-mgr --verbose-daemon
sudo power-daemon-mgr -vvv generate-configt-files --path /
sudo systemctl restart acpid
sudo systemctl daemon-reload
sudo systemctl restart power-options
92 changes: 81 additions & 11 deletions crates/power-daemon-mgr/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use clap::{Parser, Subcommand};
use clap_verbosity_flag::{InfoLevel, Verbosity};

use colored::Colorize;
use log::{debug, error, trace, Level, Log, Metadata, Record};
use log::{debug, error, info, trace, Level, Log, Metadata, Record};
use nix::unistd::Uid;

use power_daemon::{
Expand All @@ -27,7 +27,7 @@ struct Args {

#[derive(Debug, Clone, Subcommand)]
enum OpMode {
GenerateFiles {
GenerateBaseFiles {
#[arg(long)]
path: PathBuf,
/// Path of the executable for this program
Expand All @@ -37,6 +37,14 @@ enum OpMode {
#[arg(long, action=clap::ArgAction::SetTrue)]
verbose_daemon: bool,
},
GenerateConfigFiles {
#[arg(long)]
path: PathBuf,
#[arg(long, action=clap::ArgAction::SetTrue)]
config_only: bool,
#[arg(long, action=clap::ArgAction::SetTrue)]
profiles_only: bool,
},
Daemon,
RefreshFull,
RefreshUSB,
Expand Down Expand Up @@ -75,6 +83,9 @@ impl Log for StdoutLogger {
fn flush(&self) {}
}

pub const CONFIG_FILE: &str = "/etc/power-options/config.toml";
pub const PROFILES_DIRECTORY: &str = "/etc/power-options/profiles";

#[tokio::main]
async fn main() {
let args = Args::parse();
Expand All @@ -83,12 +94,67 @@ async fn main() {
log::set_max_level(args.verbose.log_level_filter());

match args.mode {
OpMode::Daemon => daemon().await,
OpMode::GenerateFiles {
OpMode::Daemon => {
let missing_config = fs::metadata(CONFIG_FILE).is_err();

let missing_profiles = if let Ok(dir) = fs::read_dir(PROFILES_DIRECTORY) {
dir.flatten().count() == 0
} else {
true
};

debug!("Config missing: {missing_config}; Profiles missing: {missing_profiles}");

if missing_config || missing_profiles {
info!("Either missing config or missing profiles were detected. Generating them now...");

let mut args = vec!["generate-config-files", "--path", "/"];
if missing_config ^ missing_profiles {
if missing_config {
args.push("--config-only")
} else {
args.push("--profiles-only")
}
}

let out = std::process::Command::new(
std::env::current_exe()
.unwrap()
.to_string_lossy()
.into_owned(),
)
.args(&args)
.output()
.expect("Failed to execute self");

if !out.status.success() {
error!(
"Issue while attempting to generate config/profiles. Exited without success: {:?}",
out.status
);
std::process::exit(-1);
}
}

daemon().await
}
OpMode::GenerateBaseFiles {
path,
program_path,
verbose_daemon,
} => generate_files(path, program_path, verbose_daemon),
} => generate_base_files(path, program_path, verbose_daemon),
OpMode::GenerateConfigFiles {
path,
mut config_only,
mut profiles_only,
} => {
if !config_only && !profiles_only {
config_only = true;
profiles_only = true;
}

generate_config_files(path, config_only, profiles_only);
}
OpMode::RefreshFull => refresh_full().await,
OpMode::RefreshUSB => refresh_reduced(ReducedUpdate::USB).await,
OpMode::RefreshPCI => {
Expand All @@ -101,9 +167,6 @@ async fn main() {
}
}

pub const CONFIG_FILE: &str = "/etc/power-options/config.toml";
pub const PROFILES_DIRECTORY: &str = "/etc/power-options/profiles";

async fn daemon() {
// From now on, we are the daemon
proctitle::set_title("power-daemon");
Expand All @@ -130,9 +193,16 @@ async fn daemon() {
}
}

fn generate_files(path: PathBuf, program_path: PathBuf, verbose_daemon: bool) {
generate_config(&path);
generate_profiles(&path);
fn generate_config_files(path: PathBuf, config: bool, profiles: bool) {
if config {
generate_config(&path);
}
if profiles {
generate_profiles(&path);
}
}

fn generate_base_files(path: PathBuf, program_path: PathBuf, verbose_daemon: bool) {
generate_udev_file(&path, &program_path);
generate_acpi_file(&path, &program_path);
generate_dbus_file(&path);
Expand Down
2 changes: 1 addition & 1 deletion install-daemon-only.sh
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ popd

sudo cp -f target/release/power-daemon-mgr /usr/bin/

sudo target/release/power-daemon-mgr -v generate-files --path / --program-path /usr/bin/power-daemon-mgr
sudo target/release/power-daemon-mgr -v generate-base-files --path / --program-path /usr/bin/power-daemon-mgr

sudo systemctl restart acpid
sudo systemctl daemon-reload
Expand Down
2 changes: 1 addition & 1 deletion install-gtk.sh
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ popd
sudo cp -f target/release/power-daemon-mgr /usr/bin/
sudo cp -f target/release/frontend-gtk /usr/bin/power-options-gtk

sudo target/release/power-daemon-mgr -v generate-files --path / --program-path /usr/bin/power-daemon-mgr
sudo target/release/power-daemon-mgr -v generate-base-files --path / --program-path /usr/bin/power-daemon-mgr

sudo systemctl restart acpid
sudo systemctl daemon-reload
Expand Down
2 changes: 1 addition & 1 deletion install-webview.sh
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ popd
sudo cp -f target/release/power-daemon-mgr /usr/bin/
sudo cp -f dist/bundle/deb/frontend_*_amd64/data/usr/bin/frontend-webview /usr/bin/power-options-webview

sudo target/release/power-daemon-mgr -v generate-files --path / --program-path /usr/bin/power-daemon-mgr
sudo target/release/power-daemon-mgr -v generate-base-files --path / --program-path /usr/bin/power-daemon-mgr

sudo systemctl restart acpid
sudo systemctl daemon-reload
Expand Down

0 comments on commit 818594e

Please sign in to comment.