From 7900bd4420c9b28647efb760dc17b1b4c3425996 Mon Sep 17 00:00:00 2001 From: fufesou Date: Thu, 26 Nov 2026 22:07:01 +0800 Subject: [PATCH 1/3] feat(config): allow bulk writes to preserve a stored option Signed-off-by: fufesou --- src/config.rs | 1 + src/config/preserved_option.rs | 37 ++++++++++++++++++++++++++++++++++ 2 files changed, 38 insertions(+) create mode 100644 src/config/preserved_option.rs diff --git a/src/config.rs b/src/config.rs index 9ee5cacde..94a166525 100644 --- a/src/config.rs +++ b/src/config.rs @@ -19,6 +19,7 @@ use sodiumoxide::base64; use sodiumoxide::crypto::sign; mod permanent_password; +mod preserved_option; pub use permanent_password::{ compute_permanent_password_h1, decode_permanent_password_h1_from_storage, diff --git a/src/config/preserved_option.rs b/src/config/preserved_option.rs new file mode 100644 index 000000000..704359474 --- /dev/null +++ b/src/config/preserved_option.rs @@ -0,0 +1,37 @@ +use super::{Config, Config2, CONFIG2}; +use std::collections::HashMap; + +impl Config2 { + fn preserve_option(&self, options: &mut HashMap, key: &str) { + if let Some(value) = self.options.get(key) { + options.insert(key.to_owned(), value.clone()); + } else { + options.remove(key); + } + } + + /// Replaces the configuration while retaining the locally stored entry for `key`. + pub fn set_preserving_option(mut incoming: Self, key: &str) -> bool { + let mut current = CONFIG2.write().unwrap(); + current.preserve_option(&mut incoming.options, key); + if *current == incoming { + return false; + } + *current = incoming; + current.store(); + true + } +} + +impl Config { + /// Replaces the options while retaining the locally stored entry for `key`. + pub fn set_options_preserving_option(mut options: HashMap, key: &str) { + Self::purify_options(&mut options); + let mut current = CONFIG2.write().unwrap(); + current.preserve_option(&mut options, key); + if current.options != options { + current.options = options; + current.store(); + } + } +} From e0e83409933c8550607e3ea94df7290997c34c49 Mon Sep 17 00:00:00 2001 From: fufesou Date: Fri, 27 Nov 2026 17:50:41 +0800 Subject: [PATCH 2/3] refactor(config): keep option-preserving setters beside the bulk setters Signed-off-by: fufesou --- src/config.rs | 32 ++++++++++++++++++++++++++++- src/config/preserved_option.rs | 37 ---------------------------------- 2 files changed, 31 insertions(+), 38 deletions(-) delete mode 100644 src/config/preserved_option.rs diff --git a/src/config.rs b/src/config.rs index 94a166525..b7ffdaf1c 100644 --- a/src/config.rs +++ b/src/config.rs @@ -19,7 +19,6 @@ use sodiumoxide::base64; use sodiumoxide::crypto::sign; mod permanent_password; -mod preserved_option; pub use permanent_password::{ compute_permanent_password_h1, decode_permanent_password_h1_from_storage, @@ -641,6 +640,26 @@ impl Config2 { lock.store(); true } + + /// Replaces the configuration while retaining the locally stored entry for `key`. + pub fn set_preserving_option(mut incoming: Self, key: &str) -> bool { + let mut current = CONFIG2.write().unwrap(); + current.preserve_option(&mut incoming.options, key); + if *current == incoming { + return false; + } + *current = incoming; + current.store(); + true + } + + fn preserve_option(&self, options: &mut HashMap, key: &str) { + if let Some(value) = self.options.get(key) { + options.insert(key.to_owned(), value.clone()); + } else { + options.remove(key); + } + } } fn keep_encrypted_storage_if_plaintext_unchanged(plain: &str, stored: &str) -> String { @@ -1379,6 +1398,17 @@ impl Config { config.store(); } + /// Replaces the options while retaining the locally stored entry for `key`. + pub fn set_options_preserving_option(mut options: HashMap, key: &str) { + Self::purify_options(&mut options); + let mut current = CONFIG2.write().unwrap(); + current.preserve_option(&mut options, key); + if current.options != options { + current.options = options; + current.store(); + } + } + pub fn get_option(k: &str) -> String { get_or( &OVERWRITE_SETTINGS, diff --git a/src/config/preserved_option.rs b/src/config/preserved_option.rs deleted file mode 100644 index 704359474..000000000 --- a/src/config/preserved_option.rs +++ /dev/null @@ -1,37 +0,0 @@ -use super::{Config, Config2, CONFIG2}; -use std::collections::HashMap; - -impl Config2 { - fn preserve_option(&self, options: &mut HashMap, key: &str) { - if let Some(value) = self.options.get(key) { - options.insert(key.to_owned(), value.clone()); - } else { - options.remove(key); - } - } - - /// Replaces the configuration while retaining the locally stored entry for `key`. - pub fn set_preserving_option(mut incoming: Self, key: &str) -> bool { - let mut current = CONFIG2.write().unwrap(); - current.preserve_option(&mut incoming.options, key); - if *current == incoming { - return false; - } - *current = incoming; - current.store(); - true - } -} - -impl Config { - /// Replaces the options while retaining the locally stored entry for `key`. - pub fn set_options_preserving_option(mut options: HashMap, key: &str) { - Self::purify_options(&mut options); - let mut current = CONFIG2.write().unwrap(); - current.preserve_option(&mut options, key); - if current.options != options { - current.options = options; - current.store(); - } - } -} From 8d6b5ff4556ad8316eac8c0e2f9a53e1173ee9e9 Mon Sep 17 00:00:00 2001 From: fufesou Date: Fri, 27 Nov 2026 18:50:44 +0800 Subject: [PATCH 3/3] feat(config): preserve multiple option keys during bulk writes Signed-off-by: fufesou --- src/config.rs | 42 +++++++++++++++++++++++++++++++----------- 1 file changed, 31 insertions(+), 11 deletions(-) diff --git a/src/config.rs b/src/config.rs index b7ffdaf1c..3cc4ab918 100644 --- a/src/config.rs +++ b/src/config.rs @@ -641,10 +641,10 @@ impl Config2 { true } - /// Replaces the configuration while retaining the locally stored entry for `key`. - pub fn set_preserving_option(mut incoming: Self, key: &str) -> bool { + /// Replaces the configuration while keeping each preserved key's stored value or absence. + pub fn set_preserving_keys(mut incoming: Self, preserved_keys: &[&str]) -> bool { let mut current = CONFIG2.write().unwrap(); - current.preserve_option(&mut incoming.options, key); + current.preserve_keys(&mut incoming.options, preserved_keys); if *current == incoming { return false; } @@ -653,11 +653,13 @@ impl Config2 { true } - fn preserve_option(&self, options: &mut HashMap, key: &str) { - if let Some(value) = self.options.get(key) { - options.insert(key.to_owned(), value.clone()); - } else { - options.remove(key); + fn preserve_keys(&self, options: &mut HashMap, preserved_keys: &[&str]) { + for &key in preserved_keys { + if let Some(value) = self.options.get(key) { + options.insert(key.to_owned(), value.clone()); + } else { + options.remove(key); + } } } } @@ -1398,11 +1400,14 @@ impl Config { config.store(); } - /// Replaces the options while retaining the locally stored entry for `key`. - pub fn set_options_preserving_option(mut options: HashMap, key: &str) { + /// Replaces the options while keeping each preserved key's stored value or absence. + pub fn set_options_preserving_keys( + mut options: HashMap, + preserved_keys: &[&str], + ) { Self::purify_options(&mut options); let mut current = CONFIG2.write().unwrap(); - current.preserve_option(&mut options, key); + current.preserve_keys(&mut options, preserved_keys); if current.options != options { current.options = options; current.store(); @@ -3532,6 +3537,21 @@ mod tests { assert_eq!(cfg.salt, "new-salt"); } + #[test] + fn preserves_multiple_option_keys() { + let mut stored = Config2::default(); + stored.options.insert("present".into(), "local".into()); + let mut incoming = HashMap::from([ + ("present".into(), "replacement".into()), + ("absent".into(), "new".into()), + ("editable".into(), "changed".into()), + ]); + stored.preserve_keys(&mut incoming, &["present", "absent"]); + assert_eq!(incoming["present"], "local"); + assert!(!incoming.contains_key("absent")); + assert_eq!(incoming["editable"], "changed"); + } + #[test] fn test_overwrite_settings() { DEFAULT_SETTINGS