mirror of
https://github.com/rustdesk/hbb_common.git
synced 2026-11-30 09:45:12 +00:00
Merge pull request #650 from fufesou/fix/preserve-ipc-reg-recovery
Fix/preserve ipc reg recovery
This commit is contained in:
1 file changed
+51
@@ -640,6 +640,28 @@ impl Config2 {
|
|||||||
lock.store();
|
lock.store();
|
||||||
true
|
true
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// 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_keys(&mut incoming.options, preserved_keys);
|
||||||
|
if *current == incoming {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
*current = incoming;
|
||||||
|
current.store();
|
||||||
|
true
|
||||||
|
}
|
||||||
|
|
||||||
|
fn preserve_keys(&self, options: &mut HashMap<String, String>, 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);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn keep_encrypted_storage_if_plaintext_unchanged(plain: &str, stored: &str) -> String {
|
fn keep_encrypted_storage_if_plaintext_unchanged(plain: &str, stored: &str) -> String {
|
||||||
@@ -1378,6 +1400,20 @@ impl Config {
|
|||||||
config.store();
|
config.store();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Replaces the options while keeping each preserved key's stored value or absence.
|
||||||
|
pub fn set_options_preserving_keys(
|
||||||
|
mut options: HashMap<String, String>,
|
||||||
|
preserved_keys: &[&str],
|
||||||
|
) {
|
||||||
|
Self::purify_options(&mut options);
|
||||||
|
let mut current = CONFIG2.write().unwrap();
|
||||||
|
current.preserve_keys(&mut options, preserved_keys);
|
||||||
|
if current.options != options {
|
||||||
|
current.options = options;
|
||||||
|
current.store();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
pub fn get_option(k: &str) -> String {
|
pub fn get_option(k: &str) -> String {
|
||||||
get_or(
|
get_or(
|
||||||
&OVERWRITE_SETTINGS,
|
&OVERWRITE_SETTINGS,
|
||||||
@@ -3516,6 +3552,21 @@ mod tests {
|
|||||||
assert_eq!(cfg.salt, "new-salt");
|
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]
|
#[test]
|
||||||
fn test_overwrite_settings() {
|
fn test_overwrite_settings() {
|
||||||
let _guard = CONFIG_STATE_TEST_LOCK.lock().unwrap();
|
let _guard = CONFIG_STATE_TEST_LOCK.lock().unwrap();
|
||||||
|
|||||||
Reference in new issue
Block a user