Jan is an open source alternative to ChatGPT that runs 100% offline on your computer.
feat(tauri): add settings_backup_once for a one-time copy of settings.json
thinhlpg6b6400d7d97d
diff --git a/src-tauri/src/core/app/settings_store.rs b/src-tauri/src/core/app/settings_store.rsindex ed3f438c7..019ff3026 100644--- a/src-tauri/src/core/app/settings_store.rs+++ b/src-tauri/src/core/app/settings_store.rs@@ -271,6 +271,51 @@ pub fn settings_flush() -> Result<(), String> {flush_settings()}+/// Copy the current settings, once, to `settings.<label>.json` beside+/// `settings.json`, before a one-way change such as the Cowork migration. An+/// existing backup is never overwritten, so it keeps the oldest state. Returns+/// whether this call wrote it.+#[tauri::command]+pub fn settings_backup_once(label: String) -> Result<bool, String> {+ let path = backup_path(&settings_file_path(), &label)?;+ backup_once_to(store(), &path)+}++fn backup_path(settings: &std::path::Path, label: &str) -> Result<PathBuf, String> {+ let valid = !label.is_empty()+ && label.len() <= 64+ && label+ .bytes()+ .all(|b| b.is_ascii_lowercase() || b.is_ascii_digit() || b == b'-');+ if !valid {+ return Err(format!("invalid settings backup label: {label:?}"));+ }+ Ok(settings.with_file_name(format!("settings.{label}.json")))+}++fn backup_once_to(store: &Store, path: &PathBuf) -> Result<bool, String> {+ // Under WRITE, like a flush: a factory reset waits for it, and one that+ // already ran leaves nothing to back up.+ let _write = WRITE.lock().unwrap_or_else(|e| e.into_inner());+ let snapshot = {+ let guard = lock(store);+ if guard.discarded {+ return Ok(false);+ }+ guard.map.clone()+ };+ if path.exists() {+ return Ok(false);+ }+ write_map_atomic(path, &snapshot)?;+ // This file is the only copy of what the change replaces: make it durable+ // before the caller goes ahead.+ fs::File::open(path)+ .and_then(|f| f.sync_all())+ .map_err(|e| e.to_string())?;+ Ok(true)+}+#[cfg(test)]mod tests {use super::*;@@ -422,4 +467,36 @@ mod tests {fs::write(&empty, " ").unwrap();assert!(read_map(&empty).is_empty());}++ #[test]+ fn backup_is_written_once_and_never_overwritten() {+ let dir = tempfile::tempdir().unwrap();+ let path = backup_path(&dir.path().join("settings.json"), "pre-x").unwrap();+ assert!(path.ends_with("settings.pre-x.json"));+ let store: Store = (Mutex::new(new_map()), Condvar::new());+ store.0.lock().unwrap().set("code-sessions".into(), "old".into());++ assert_eq!(backup_once_to(&store, &path), Ok(true));+ store.0.lock().unwrap().set("code-sessions".into(), "new".into());+ assert_eq!(backup_once_to(&store, &path), Ok(false));+ assert_eq!(read_map(&path).get("code-sessions"), Some(&"old".to_string()));+ }++ #[test]+ fn backup_label_cannot_leave_the_data_folder_or_replace_settings() {+ let settings = std::path::Path::new("/data/settings.json");+ for bad in ["", "../x", "a/b", "A", "x.json", "a\\b"] {+ assert!(backup_path(settings, bad).is_err(), "{bad:?}");+ }+ }++ #[test]+ fn no_backup_after_a_factory_reset() {+ let dir = tempfile::tempdir().unwrap();+ let path = dir.path().join("settings.pre-x.json");+ let store: Store = (Mutex::new(new_map()), Condvar::new());+ store.0.lock().unwrap().discard();+ assert_eq!(backup_once_to(&store, &path), Ok(false));+ assert!(!path.exists());+ }}diff --git a/src-tauri/src/lib.rs b/src-tauri/src/lib.rsindex e9a3fc853..067dc376e 100644--- a/src-tauri/src/lib.rs+++ b/src-tauri/src/lib.rs@@ -67,6 +67,7 @@ macro_rules! invoke_commands_with_extras {core::app::settings_store::settings_set,core::app::settings_store::settings_remove,core::app::settings_store::settings_flush,+ core::app::settings_store::settings_backup_once,core::server::provider_secrets::set_secret,core::server::provider_secrets::get_secret,// System commands