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| {
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(

View file

@ -31,15 +31,20 @@ impl Default for SettingsJson {
impl SettingsJson {
pub fn save<P: AsRef<std::path::Path>>(&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(())
}
}
}

View file

@ -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!");
});

View file

@ -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<bool, SettingError> {
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 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;
}

View file

@ -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"
}
}