The truly free, open-source, local-first, cross-platform CapCut replacement. Scriptable through a JSON-RPC API & MCP.
Rust ffmpegfree-video-editormodel-context-protocolnon-linear-editoroffline-firstopen-source-video-editorself-hostedvideo-editingvideo-editing-automationvideo-editing-softwarevideo-editing-toolsvideo-editorvoice-cloningyoutube-automationyoutube-shorts
fix(prefs): settings are written whole and for this account alone, and a bad value costs only itself
Jareer4b1b2b0507dc
diff --git a/src/crates/concat/src/prefs.rs b/src/crates/concat/src/prefs.rsindex 195740fb..da7a4a70 100644--- a/src/crates/concat/src/prefs.rs+++ b/src/crates/concat/src/prefs.rs@@ -322,22 +322,83 @@ impl Preferences {/// Reads the file, or the defaults when there is none.pub fn load(dirs: &AppDirs) -> Self {- std::fs::read(dirs.config.join(FILE))- .ok()- .and_then(|bytes| serde_json::from_slice(&bytes).ok())- .unwrap_or_default()+ let path = dirs.config.join(FILE);+ let Ok(bytes) = std::fs::read(&path) else {+ return Self::default();+ };+ if let Ok(preferences) = serde_json::from_slice(&bytes) {+ return preferences;+ }+ // One value of the wrong shape - a field an older or newer build+ // wrote differently, a hand edit - failed the whole file, and every+ // setting went back to its default at once. The ones that still+ // read are kept, and the file as it was is set aside rather than+ // overwritten by the next save.+ let _ = std::fs::copy(&path, path.with_extension("json.unreadable"));+ let preferences = salvage(&bytes);+ log::warn!(+ "settings: {} did not read whole; kept what did",+ path.display()+ );+ preferences}/// Writes the file. Best effort: a preference that did not stick is- /// not worth interrupting anyone over.+ /// not worth interrupting anyone over. Written beside the file and+ /// moved over it, so a crash or a second window saving at the same+ /// moment leaves one whole file, never half of one; and readable by+ /// this account alone where the platform has the notion, since it+ /// holds the remote API's token.pub fn save(&self, dirs: &AppDirs) {let _ = std::fs::create_dir_all(&dirs.config);- if let Ok(encoded) = serde_json::to_vec_pretty(self) {- let _ = std::fs::write(dirs.config.join(FILE), encoded);+ let Ok(encoded) = serde_json::to_vec_pretty(self) else {+ return;+ };+ let path = dirs.config.join(FILE);+ let staging = dirs+ .config+ .join(format!("{FILE}.{}.part", std::process::id()));+ if write_private(&staging, &encoded).is_err() || std::fs::rename(&staging, &path).is_err() {+ let _ = std::fs::remove_file(&staging);}}}+/// The fields of a settings file that read, on the defaults: each top-level+/// value is laid over them in turn and kept only if the whole still reads.+fn salvage(bytes: &[u8]) -> Preferences {+ let Ok(serde_json::Value::Object(found)) = serde_json::from_slice(bytes) else {+ return Preferences::default();+ };+ let Ok(serde_json::Value::Object(mut kept)) = serde_json::to_value(Preferences::default())+ else {+ return Preferences::default();+ };+ for (key, value) in found {+ let before = kept.insert(key.clone(), value);+ if serde_json::from_value::<Preferences>(serde_json::Value::Object(kept.clone())).is_err() {+ match before {+ Some(before) => kept.insert(key, before),+ None => kept.remove(&key),+ };+ }+ }+ serde_json::from_value(serde_json::Value::Object(kept)).unwrap_or_default()+}++/// Writes a new file only this account can read, on Unix; elsewhere as+/// the platform makes files, which on Windows is the profile's own folder.+fn write_private(path: &std::path::Path, bytes: &[u8]) -> std::io::Result<()> {+ use std::io::Write;+ let mut options = std::fs::OpenOptions::new();+ options.write(true).create(true).truncate(true);+ #[cfg(unix)]+ std::os::unix::fs::OpenOptionsExt::mode(&mut options, 0o600);+ let mut file = options.open(path)?;+ file.write_all(bytes)?;+ file.sync_all()+}+#[cfg(test)]mod tests {use super::*;@@ -359,6 +420,51 @@ mod tests {assert_eq!(old.magnetic(), crate::platform::phone());}+ /// One value of the wrong shape costs that value, not every setting,+ /// and the file is set aside before anything writes over it.+ #[test]+ fn a_settings_file_that_does_not_read_whole_keeps_what_does() {+ let root =+ std::env::temp_dir().join(format!("concat-settings-salvage-{}", std::process::id()));+ let dirs = AppDirs {+ config: root.join("config"),+ data: root.join("data"),+ };+ std::fs::create_dir_all(&dirs.config).expect("a config folder");+ std::fs::write(+ dirs.config.join(FILE),+ r#"{"magnetic": true, "dark": "very", "lastProjectLocation": "/films"}"#,+ )+ .expect("a settings file");+ let prefs = Preferences::load(&dirs);+ assert!(prefs.magnetic);+ assert_eq!(prefs.last_project_location.as_deref(), Some("/films"));+ assert_eq!(+ prefs.dark, None,+ "the value that did not read is the default"+ );+ assert!(dirs.config.join("settings.json.unreadable").is_file());++ prefs.save(&dirs);+ assert!(Preferences::load(&dirs).magnetic);+ #[cfg(unix)]+ {+ use std::os::unix::fs::PermissionsExt;+ let mode = std::fs::metadata(dirs.config.join(FILE))+ .expect("saved")+ .permissions()+ .mode();+ assert_eq!(mode & 0o077, 0, "nobody else reads it");+ }+ let leftovers = std::fs::read_dir(&dirs.config)+ .expect("listed")+ .filter_map(Result::ok)+ .filter(|entry| entry.file_name().to_string_lossy().ends_with(".part"))+ .count();+ assert_eq!(leftovers, 0);+ let _ = std::fs::remove_dir_all(root);+ }+#[test]fn the_last_project_location_survives_loading_and_saving() {let root =