Skip to content

Commit

Permalink
fix: prevent thread blocking forever from reading config
Browse files Browse the repository at this point in the history
  • Loading branch information
oskardotglobal committed Nov 4, 2024
1 parent 69ca359 commit f745e36
Show file tree
Hide file tree
Showing 7 changed files with 40 additions and 30 deletions.
3 changes: 2 additions & 1 deletion default.nix
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
let
manifest = (pkgs.lib.importTOML ./winapps-cli/Cargo.toml).package;
in
pkgs.rustPlatform.buildRustPackage rec {
pkgs.rustPlatform.buildRustPackage {
pname = manifest.name;
version = manifest.version;
cargoLock.lockFile = ./Cargo.lock;
Expand All @@ -16,4 +16,5 @@ pkgs.rustPlatform.buildRustPackage rec {
PKG_CONFIG_PATH = "${pkgs.openssl.dev}/lib/pkgconfig";

propagatedBuildInputs = with pkgs; [ freerdp3 ];
wrapperArgs = [ ];
}
3 changes: 2 additions & 1 deletion winapps-cli/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,14 @@ fn main() -> Result<()> {
// .with_timer(tracing_subscriber::fmt::time::uptime())
.without_time()
.with_target(false)
.with_level(true)
.init();

let cli = cli();
let matches = cli.clone().get_matches();

Config::load(None)?;
let config = Config::get();
config.load(None)?;

let client = Freerdp::new();
let backend = config.get_backend();
Expand Down
5 changes: 3 additions & 2 deletions winapps/src/backend/docker.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
use crate::command::execute_str;
use crate::{ensure, Backend, Config, Error, Result};
use std::sync::RwLockReadGuard;

use crate::{command::execute_str, ensure, Backend, Config, Error, Result};

#[derive(Debug)]
pub struct Docker {
config: RwLockReadGuard<'static, Config>,
}
Expand Down
10 changes: 5 additions & 5 deletions winapps/src/backend/mod.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
mod docker;
use std::fmt::Debug;

use crate::{backend::docker::Docker, Config, Result};

use crate::backend::docker::Docker;
use crate::config::Config;
use crate::errors::Result;
mod docker;

pub trait Backend {
pub trait Backend: Debug {
fn check_depends(&self) -> Result<()>;

fn get_host(&self) -> String;
Expand Down
3 changes: 1 addition & 2 deletions winapps/src/config/apps.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
use crate::config::App;
use crate::Config;
use crate::{config::App, Config};

impl PartialEq for App {
fn eq(&self, other: &Self) -> bool {
Expand Down
42 changes: 25 additions & 17 deletions winapps/src/config/operations.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ use std::io::Write;
use std::path::{Path, PathBuf};
use std::sync::{LazyLock, RwLock, RwLockReadGuard};

use tracing::warn;

use crate::{bail, Config, Error, IntoResult, Result};

fn path_ok(path: &Path) -> Result<()> {
Expand All @@ -27,25 +29,17 @@ impl Config {
CONFIG.read().unwrap()
}

fn get_path(path: Option<&str>) -> Result<PathBuf> {
let path = match (path, dirs::config_dir()) {
(Some(path), _) => Ok(PathBuf::from(path)),
(None, Some(path)) => Ok(path),
_ => "Could not find $XDG_CONFIG_HOME and no config path specified".into_result(),
}
.map(|path| path.join("winapps").join("config.toml"))?;

let parent = path.parent().unwrap();
path_ok(parent)?;

Ok(path)
}

pub fn load(&self, path: Option<&str>) -> Result<()> {
/// Reads the config from disk.
///
/// Note: Since this uses a RwLock under the hood, call this before aquiring any read locks
/// (ensure `Config::get` isn't called anywhere unless the config is already loaded)
/// TODO: This behaviour is not ideal.
pub fn load(path: Option<&str>) -> Result<()> {
let config_path = Self::get_path(path)?;

if !config_path.exists() {
return self.save(path);
warn!("Config does not exist, writing default...");
return CONFIG.read().unwrap().save(path);
}

let config_file = fs::read_to_string(config_path).into_result()?;
Expand All @@ -55,12 +49,26 @@ impl Config {
bail!(Error::Config("More than one backend enabled, please set only one of libvirt.enable, container.enable, and manual.enable"));
}

let mut global_config = CONFIG.write().unwrap();
let mut global_config = CONFIG.try_write().unwrap();
*global_config = config;

Ok(())
}

fn get_path(path: Option<&str>) -> Result<PathBuf> {
let path = match (path, dirs::config_dir()) {
(Some(path), _) => Ok(PathBuf::from(path)),
(None, Some(path)) => Ok(path),
_ => "Could not find $XDG_CONFIG_HOME and no config path specified".into_result(),
}
.map(|path| path.join("winapps").join("config.toml"))?;

let parent = path.parent().unwrap();
path_ok(parent)?;

Ok(path)
}

pub fn save(&self, path: Option<&str>) -> Result<()> {
let config_path = Self::get_path(path)?;
let serialized_config = toml::to_string(&self).into_result()?;
Expand Down
4 changes: 2 additions & 2 deletions winapps/src/remote_client/freerdp.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
use std::process::{Command, Stdio};
use std::sync::RwLockReadGuard;

use tracing::{info, warn};

use crate::command::execute;
use crate::{Backend, Config, RemoteClient, Result};
use crate::{command::execute, Backend, Config, RemoteClient, Result};

pub struct Freerdp {
config: RwLockReadGuard<'static, Config>,
Expand Down

0 comments on commit f745e36

Please sign in to comment.