Handling of home dir when running as root

This commit is contained in:
cyber-sushi 2024-05-07 19:49:39 +02:00
parent 0a931e671f
commit 7239372900

View file

@ -6,7 +6,6 @@ mod active_client;
use std::env; use std::env;
use tokio; use tokio;
use home;
use config::Config; use config::Config;
use tokio::task::JoinHandle; use tokio::task::JoinHandle;
use crate::udev_monitor::*; use crate::udev_monitor::*;
@ -14,25 +13,37 @@ use crate::udev_monitor::*;
#[tokio::main] #[tokio::main]
async fn main() { async fn main() {
let default_config_path = format!("{}/.config/makima", home::home_dir().unwrap().display()); let user = match env::var("USER") {
let env_path: String = match env::var("MAKIMA_CONFIG") { Ok(user) if user == "root".to_string() => {
Ok(path) => { match env::var("SUDO_USER") {
println!("\nConfig directory set to {:?}.", path); Ok(sudo_user) => format!("/home/{}", sudo_user),
path _ => format!("/{}", user),
}, }
Err(_) => {
println!("\n\"MAKIMA_CONFIG\" environment variable is not set, defaulting to {:?}.", default_config_path);
default_config_path.clone()
}, },
Ok(user) => format!("/home/{}", user),
_ => "/root".to_string(),
}; };
let config_path: std::fs::ReadDir = match std::fs::read_dir(&env_path) { let default_config_path = format!("{}/.config/makima", user);
Ok(config_path) => { let config_path = match env::var("MAKIMA_CONFIG") {
println!("Scanning for config files...\n"); Ok(path) => {
config_path println!("\nMAKIMA_CONFIG set to {:?}.", path);
match std::fs::read_dir(path) {
Ok(dir) => dir,
_ => {
println!("Directory not found, exiting Makima.");
std::process::exit(0);
}
}
}, },
Err(_) => { Err(_) => {
println!("Directory not found, falling back to {:?}.\n", default_config_path); println!("\nMAKIMA_CONFIG environment variable is not set, defaulting to {:?}.", default_config_path);
std::fs::read_dir(default_config_path).unwrap() match std::fs::read_dir(default_config_path) {
Ok(dir) => dir,
_ => {
println!("Directory not found, exiting Makima.");
std::process::exit(0);
}
}
}, },
}; };
let mut config_files: Vec<Config> = Vec::new(); let mut config_files: Vec<Config> = Vec::new();