Test and perfect fix for non-persistent saving

This commit is contained in:
NGnius (Graham) 2022-10-11 17:38:20 -04:00
parent c13f35a4a1
commit 357a7cfe37
5 changed files with 62 additions and 33 deletions

View file

@ -48,7 +48,7 @@ pub fn load_settings(
move |params_in: super::ApiParameterType| { move |params_in: super::ApiParameterType| {
if let Some(Primitive::String(path)) = params_in.get(0) { if let Some(Primitive::String(path)) = params_in.get(0) {
if let Some(Primitive::String(name)) = params_in.get(1) { 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()], Err(e) => vec![e.msg.into()],
Ok(success) => Ok(success) =>
super::utility::map_empty_result( super::utility::map_empty_result(
@ -73,7 +73,8 @@ pub fn load_default_settings(
move |_: super::ApiParameterType| { move |_: super::ApiParameterType| {
match settings.load_file( match settings.load_file(
crate::consts::DEFAULT_SETTINGS_FILE.into(), 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()], Err(e) => vec![e.msg.into()],
Ok(success) => super::utility::map_empty_result( Ok(success) => super::utility::map_empty_result(

View file

@ -31,15 +31,20 @@ impl Default for SettingsJson {
impl SettingsJson { impl SettingsJson {
pub fn save<P: AsRef<std::path::Path>>(&self, path: P) -> Result<(), JsonError> { pub fn save<P: AsRef<std::path::Path>>(&self, path: P) -> Result<(), JsonError> {
let path = path.as_ref(); 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. if self.persistent {
std::fs::remove_file(path).map_err(JsonError::Io)
} else {
if let Some(parent) = path.parent() { if let Some(parent) = path.parent() {
std::fs::create_dir_all(parent).map_err(JsonError::Io)?; std::fs::create_dir_all(parent).map_err(JsonError::Io)?;
} }
let mut file = std::fs::File::create(path).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) 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(())
}
} }
} }

View file

@ -11,12 +11,17 @@ pub fn spawn(settings: Settings) -> (JoinHandle<()>, Sender<()>) {
log::info!("save_worker starting..."); log::info!("save_worker starting...");
for _ in receiver.iter() { for _ in receiver.iter() {
log::debug!("save_worker is saving..."); log::debug!("save_worker is saving...");
let is_persistent = unwrap_lock(settings.general.lock(), "general").persistent.clone();
if is_persistent {
let save_path = crate::utility::settings_dir() let save_path = crate::utility::settings_dir()
.join(unwrap_lock(settings.general.lock(), "general").path.clone()); .join(unwrap_lock(settings.general.lock(), "general").path.clone());
let settings_clone = settings.clone(); let settings_clone = settings.clone();
let save_json: SettingsJson = settings_clone.into(); let save_json: SettingsJson = settings_clone.into();
unwrap_maybe_fatal(save_json.save(&save_path), "Failed to save settings"); unwrap_maybe_fatal(save_json.save(&save_path), "Failed to save settings");
log::debug!("Saved settings to {}", save_path.display()); log::debug!("Saved settings to {}", save_path.display());
} else {
log::debug!("Ignored save request for non-persistent settings");
}
} }
log::warn!("save_worker completed!"); log::warn!("save_worker completed!");
}); });

View file

@ -128,7 +128,22 @@ impl Settings {
} }
} }
pub fn load_file(&self, filename: PathBuf, name: String) -> Result<bool, SettingError> { 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, system_defaults: bool) -> Result<bool, SettingError> {
let json_path = crate::utility::settings_dir().join(filename); let json_path = crate::utility::settings_dir().join(filename);
let mut general_lock = unwrap_lock(self.general.lock(), "general"); let mut general_lock = unwrap_lock(self.general.lock(), "general");
if json_path.exists() { if json_path.exists() {
@ -137,17 +152,16 @@ impl Settings {
setting: SettingVariant::General, setting: SettingVariant::General,
})?; })?;
if !settings_json.persistent { 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.persistent = false;
general_lock.name = settings_json.name; general_lock.name = name;
return Ok(false); } else {
}
let new_cpus = Self::convert_cpus(settings_json.cpus, settings_json.version); 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_gpu = Gpu::from_json(settings_json.gpu, settings_json.version);
let new_battery = Battery::from_json(settings_json.battery, 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"); 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? *cpu_lock = new_cpus;
} }
{ {
let mut gpu_lock = unwrap_lock(self.gpu.lock(), "gpu"); let mut gpu_lock = unwrap_lock(self.gpu.lock(), "gpu");
@ -159,7 +173,11 @@ impl Settings {
} }
general_lock.persistent = true; general_lock.persistent = true;
general_lock.name = settings_json.name; general_lock.name = settings_json.name;
}
} else { } else {
if system_defaults {
self.load_system_default();
}
general_lock.persistent = false; general_lock.persistent = false;
general_lock.name = name; general_lock.name = name;
} }

View file

@ -5,7 +5,7 @@
"publish": { "publish": {
"discord_id": "106537989684887552", "discord_id": "106537989684887552",
"description": "Power tweaks for power users", "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" "image": "https://raw.githubusercontent.com/NGnius/PowerTools/main/assets/thumbnail.png"
} }
} }