From 357a7cfe378bd55ed216d2f46dd9d6b5dc37ff0d Mon Sep 17 00:00:00 2001 From: "NGnius (Graham)" Date: Tue, 11 Oct 2022 17:38:20 -0400 Subject: [PATCH] Test and perfect fix for non-persistent saving --- backend/src/api/general.rs | 5 +-- backend/src/persist/general.rs | 13 +++++--- backend/src/save_worker.rs | 17 ++++++---- backend/src/settings/general.rs | 58 +++++++++++++++++++++------------ plugin.json | 2 +- 5 files changed, 62 insertions(+), 33 deletions(-) diff --git a/backend/src/api/general.rs b/backend/src/api/general.rs index cd58e3f..4da5021 100644 --- a/backend/src/api/general.rs +++ b/backend/src/api/general.rs @@ -48,7 +48,7 @@ pub fn load_settings( move |params_in: super::ApiParameterType| { if let Some(Primitive::String(path)) = params_in.get(0) { if let Some(Primitive::String(name)) = params_in.get(1) { - match settings.load_file(path.into(), name.to_owned()) { + match settings.load_file(path.into(), name.to_owned(), false) { Err(e) => vec![e.msg.into()], Ok(success) => super::utility::map_empty_result( @@ -73,7 +73,8 @@ pub fn load_default_settings( move |_: super::ApiParameterType| { match settings.load_file( crate::consts::DEFAULT_SETTINGS_FILE.into(), - crate::consts::DEFAULT_SETTINGS_NAME.to_owned() + crate::consts::DEFAULT_SETTINGS_NAME.to_owned(), + true ) { Err(e) => vec![e.msg.into()], Ok(success) => super::utility::map_empty_result( diff --git a/backend/src/persist/general.rs b/backend/src/persist/general.rs index f1183f7..4ae3c89 100644 --- a/backend/src/persist/general.rs +++ b/backend/src/persist/general.rs @@ -31,15 +31,20 @@ impl Default for SettingsJson { impl SettingsJson { pub fn save>(&self, path: P) -> Result<(), JsonError> { let path = path.as_ref(); - if !self.persistent && path.exists() { - // remove settings file when persistence is turned off, to prevent it from be loaded next time. - std::fs::remove_file(path).map_err(JsonError::Io) - } else { + + if self.persistent { if let Some(parent) = path.parent() { std::fs::create_dir_all(parent).map_err(JsonError::Io)?; } let mut file = std::fs::File::create(path).map_err(JsonError::Io)?; serde_json::to_writer_pretty(&mut file, &self).map_err(JsonError::Serde) + } else { + if path.exists() { + // remove settings file when persistence is turned off, to prevent it from be loaded next time. + std::fs::remove_file(path).map_err(JsonError::Io) + } else { + Ok(()) + } } } diff --git a/backend/src/save_worker.rs b/backend/src/save_worker.rs index bad505a..06b8a28 100644 --- a/backend/src/save_worker.rs +++ b/backend/src/save_worker.rs @@ -11,12 +11,17 @@ pub fn spawn(settings: Settings) -> (JoinHandle<()>, Sender<()>) { log::info!("save_worker starting..."); for _ in receiver.iter() { log::debug!("save_worker is saving..."); - let save_path = crate::utility::settings_dir() - .join(unwrap_lock(settings.general.lock(), "general").path.clone()); - let settings_clone = settings.clone(); - let save_json: SettingsJson = settings_clone.into(); - unwrap_maybe_fatal(save_json.save(&save_path), "Failed to save settings"); - log::debug!("Saved settings to {}", save_path.display()); + let is_persistent = unwrap_lock(settings.general.lock(), "general").persistent.clone(); + if is_persistent { + let save_path = crate::utility::settings_dir() + .join(unwrap_lock(settings.general.lock(), "general").path.clone()); + let settings_clone = settings.clone(); + let save_json: SettingsJson = settings_clone.into(); + unwrap_maybe_fatal(save_json.save(&save_path), "Failed to save settings"); + log::debug!("Saved settings to {}", save_path.display()); + } else { + log::debug!("Ignored save request for non-persistent settings"); + } } log::warn!("save_worker completed!"); }); diff --git a/backend/src/settings/general.rs b/backend/src/settings/general.rs index 6249c9b..5c40ea4 100644 --- a/backend/src/settings/general.rs +++ b/backend/src/settings/general.rs @@ -127,8 +127,23 @@ impl Settings { battery: Arc::new(Mutex::new(Battery::system_default())), } } + + fn load_system_default(&self) { + { + let mut cpu_lock = unwrap_lock(self.cpus.lock(), "cpu"); + *cpu_lock = Cpu::system_default(); + } + { + let mut gpu_lock = unwrap_lock(self.gpu.lock(), "gpu"); + *gpu_lock = Gpu::system_default(); + } + { + let mut battery_lock = unwrap_lock(self.battery.lock(), "battery"); + *battery_lock = Battery::system_default(); + } + } - pub fn load_file(&self, filename: PathBuf, name: String) -> Result { + pub fn load_file(&self, filename: PathBuf, name: String, system_defaults: bool) -> Result { let json_path = crate::utility::settings_dir().join(filename); let mut general_lock = unwrap_lock(self.general.lock(), "general"); if json_path.exists() { @@ -137,29 +152,32 @@ impl Settings { setting: SettingVariant::General, })?; if !settings_json.persistent { - log::warn!("Loaded persistent config `{}` with persistent=false", json_path.display()); + log::warn!("Loaded persistent config `{}` ({}) with persistent=false", &settings_json.name, json_path.display()); general_lock.persistent = false; + general_lock.name = name; + } else { + let new_cpus = Self::convert_cpus(settings_json.cpus, settings_json.version); + let new_gpu = Gpu::from_json(settings_json.gpu, settings_json.version); + let new_battery = Battery::from_json(settings_json.battery, settings_json.version); + { + let mut cpu_lock = unwrap_lock(self.cpus.lock(), "cpu"); + *cpu_lock = new_cpus; + } + { + let mut gpu_lock = unwrap_lock(self.gpu.lock(), "gpu"); + *gpu_lock = new_gpu; + } + { + let mut battery_lock = unwrap_lock(self.battery.lock(), "battery"); + *battery_lock = new_battery; + } + general_lock.persistent = true; general_lock.name = settings_json.name; - return Ok(false); } - let new_cpus = Self::convert_cpus(settings_json.cpus, settings_json.version); - let new_gpu = Gpu::from_json(settings_json.gpu, settings_json.version); - let new_battery = Battery::from_json(settings_json.battery, settings_json.version); - { - let mut cpu_lock = unwrap_lock(self.cpus.lock(), "cpu"); - *cpu_lock = new_cpus; // TODO does this overwrite the contents of the lock as expected? - } - { - let mut gpu_lock = unwrap_lock(self.gpu.lock(), "gpu"); - *gpu_lock = new_gpu; - } - { - let mut battery_lock = unwrap_lock(self.battery.lock(), "battery"); - *battery_lock = new_battery; - } - general_lock.persistent = true; - general_lock.name = settings_json.name; } else { + if system_defaults { + self.load_system_default(); + } general_lock.persistent = false; general_lock.name = name; } diff --git a/plugin.json b/plugin.json index 09990e9..f1e4d84 100644 --- a/plugin.json +++ b/plugin.json @@ -5,7 +5,7 @@ "publish": { "discord_id": "106537989684887552", "description": "Power tweaks for power users", - "tags": [ "utility", "power-management" ], + "tags": [ "utility", "power-management", "root" ], "image": "https://raw.githubusercontent.com/NGnius/PowerTools/main/assets/thumbnail.png" } }