diff --git a/packages/dashboard/src-tauri/src/config.rs b/packages/dashboard/src-tauri/src/config.rs index 2f548f9076..a7395718aa 100644 --- a/packages/dashboard/src-tauri/src/config.rs +++ b/packages/dashboard/src-tauri/src/config.rs @@ -215,12 +215,30 @@ fn write_config_atomic( // to the file it points at. Project configs never get here as symlinks: // they are untrusted repository content and the project validation // refuses a symlinked target. + // The backup copies get the name of the config path, not of the link + // target, so that a link that changes to a different file keeps one set + // of copies, which pruning keeps at its limit. + let logical_path = path; let path = if project_root.is_none() { resolve_symlinked_config(path)? } else { path.to_path_buf() }; let path = path.as_path(); + let backup_dir = config_backup_dir()?; + let backup_scope = match project_root { + Some(root) => format!("project-{}", project_hash(root)), + None => "user".to_string(), + }; + // Hold the lock of this config's backup set from before the read of the + // current config until after the replace. Two saves that overlap could + // otherwise both copy the same old version, and the version between them + // would have no copy. + let scope_mutex = in_process_scope_lock(&backup_dir, &backup_scope); + let _in_process = scope_mutex + .lock() + .unwrap_or_else(|poison| poison.into_inner()); + let _cross_process = lock_backup_scope(&backup_dir, &backup_scope, CONFIG_LOCK_WAIT)?; let parent = path .parent() .ok_or_else(|| "Config path has no parent directory".to_string())?; @@ -251,6 +269,12 @@ fn write_config_atomic( return Err(e); } } + // Make the copy before the rename replaces the file. If there is no copy, + // do not save. + if let Err(e) = back_up_current_config(path, logical_path, &backup_dir, &backup_scope) { + let _ = std::fs::remove_file(&temp_path); + return Err(e); + } replace_with_temp(&temp_path, path).map_err(|e| { let _ = std::fs::remove_file(&temp_path); @@ -281,6 +305,368 @@ fn resolve_symlinked_config(path: &Path) -> Result { Err("Config path has too many levels of symlinks".to_string()) } +/// The longest time that a save waits for a different dashboard process to end +/// its save of the same config. +const CONFIG_LOCK_WAIT: std::time::Duration = std::time::Duration::from_secs(10); + +/// The lock file of one backup set: `..lock` in the backup folder. +fn backup_scope_lock_path(backup_dir: &Path, scope: &str) -> PathBuf { + backup_dir.join(format!(".{scope}.lock")) +} + +/// One mutex for each backup set in this process. The file lock alone does not +/// serialize threads on platforms that have no `flock`, and saves of different +/// configs do not wait for each other. +fn in_process_scope_lock(backup_dir: &Path, scope: &str) -> std::sync::Arc> { + use std::collections::HashMap; + use std::sync::{Arc, Mutex, OnceLock}; + + static LOCKS: OnceLock>>>> = OnceLock::new(); + let mut locks = LOCKS + .get_or_init(|| Mutex::new(HashMap::new())) + .lock() + .unwrap_or_else(|poison| poison.into_inner()); + Arc::clone( + locks + .entry(backup_scope_lock_path(backup_dir, scope)) + .or_default(), + ) +} + +/// Take the exclusive lock of one backup set, so that a save in a different +/// dashboard process waits. The lock stays until the returned file closes. +/// If the lock is not free in `wait`, the save is refused, as when no copy can +/// be made. +#[cfg(unix)] +fn lock_backup_scope( + backup_dir: &Path, + scope: &str, + wait: std::time::Duration, +) -> Result, String> { + use std::os::unix::fs::{OpenOptionsExt, PermissionsExt}; + use std::os::unix::io::AsRawFd; + + let refuse = |e: std::io::Error| { + format!( + "Refusing to save config: cannot lock the backup folder {}: {e}", + backup_dir.display() + ) + }; + ensure_private_backup_directory(backup_dir).map_err(|e| { + format!( + "Refusing to save config: cannot use the backup folder {}: {e}", + backup_dir.display() + ) + })?; + // O_NOFOLLOW: a symlink at the lock path must not open a different file. + let file = std::fs::OpenOptions::new() + .read(true) + .write(true) + .create(true) + .truncate(false) + .custom_flags(libc::O_NOFOLLOW) + .mode(0o600) + .open(backup_scope_lock_path(backup_dir, scope)) + .map_err(refuse)?; + let metadata = file.metadata().map_err(refuse)?; + if !metadata.is_file() { + return Err(refuse(std::io::Error::other( + "the lock path is not a regular file", + ))); + } + if metadata.permissions().mode() & 0o777 != 0o600 { + file.set_permissions(std::fs::Permissions::from_mode(0o600)) + .map_err(refuse)?; + } + let deadline = std::time::Instant::now() + wait; + loop { + if unsafe { libc::flock(file.as_raw_fd(), libc::LOCK_EX | libc::LOCK_NB) } == 0 { + return Ok(Some(file)); + } + let error = std::io::Error::last_os_error(); + match error.raw_os_error() { + Some(libc::EINTR) => continue, + Some(code) if code == libc::EWOULDBLOCK => {} + _ => return Err(refuse(error)), + } + if std::time::Instant::now() >= deadline { + return Err(refuse(std::io::Error::other( + "a different save of this config did not end in time", + ))); + } + std::thread::sleep(std::time::Duration::from_millis(20)); + } +} + +/// Without `flock`, only the in-process mutex serializes saves. Saves in +/// different dashboard processes can then overlap. +#[cfg(not(unix))] +fn lock_backup_scope( + _backup_dir: &Path, + _scope: &str, + _wait: std::time::Duration, +) -> Result, String> { + Ok(None) +} + +/// Number of copies kept for each config in the backup folder. +const CONFIG_BACKUP_LIMIT: usize = 5; + +/// The folder for config copies: `config-backups` in the Magic Context store. +/// This function uses the same rule as the plugin to find the store +/// (`getMagicContextStorageResolution` in `packages/plugin/src/shared/data-path.ts`): +/// `MAGIC_CONTEXT_STORAGE_DIR`, then `XDG_DATA_HOME`, then `~/.local/share`, +/// on all platforms. Thus the copies stay out of the config folder, which must +/// not get more files, and out of project repositories. +fn config_backup_dir() -> Result { + let non_empty = |key: &str| { + std::env::var(key) + .ok() + .map(|value| value.trim().to_string()) + .filter(|value| !value.is_empty()) + }; + let store = if let Some(explicit) = non_empty("MAGIC_CONTEXT_STORAGE_DIR") { + let explicit = PathBuf::from(explicit); + if !explicit.is_absolute() { + return Err( + "Refusing to save config: MAGIC_CONTEXT_STORAGE_DIR is not an absolute path, \ + so there is no folder for the backup copy" + .to_string(), + ); + } + explicit + } else { + let data_home = match non_empty("XDG_DATA_HOME") { + Some(value) => PathBuf::from(value), + None => dirs::home_dir() + .ok_or_else(|| { + "Refusing to save config: no home folder, \ + so there is no folder for the backup copy" + .to_string() + })? + .join(".local") + .join("share"), + }; + data_home.join("cortexkit").join("magic-context") + }; + Ok(store.join("config-backups")) +} + +/// A short, stable name part for a project. It keeps the copies of different +/// project configs apart, and it keeps the project path out of file names. +fn project_hash(canonical_project: &Path) -> String { + use sha2::{Digest, Sha256}; + + let digest = Sha256::digest(canonical_project.to_string_lossy().as_bytes()); + digest.iter().take(6).map(|b| format!("{b:02x}")).collect() +} + +/// Copy the current config at `path` into the backup folder, then remove the +/// oldest copies of the same config. The copy names come from `logical_path`, +/// the config path before symlinks resolve. If there is no config, there is +/// no copy. +fn back_up_current_config( + path: &Path, + logical_path: &Path, + backup_dir: &Path, + scope: &str, +) -> Result<(), String> { + let Some(current) = read_current_config_no_follow(path).map_err(|e| { + format!("Refusing to save config: cannot read the current config for a backup copy: {e}") + })? + else { + return Ok(()); + }; + #[cfg(test)] + run_after_backup_read_hook(path); + ensure_private_backup_directory(backup_dir).map_err(|e| { + format!( + "Refusing to save config: cannot use the backup folder {}: {e}", + backup_dir.display() + ) + })?; + + let stem = logical_path + .file_stem() + .and_then(|name| name.to_str()) + .filter(|name| !name.is_empty()) + .unwrap_or("magic-context"); + let suffix = logical_path + .extension() + .and_then(|ext| ext.to_str()) + .map(|ext| format!(".{ext}")) + .unwrap_or_default(); + let prefix = format!("{stem}.{scope}."); + write_backup_copy(backup_dir, &prefix, &suffix, ¤t).map_err(|e| { + format!( + "Refusing to save config: cannot write the backup copy in {}: {e}", + backup_dir.display() + ) + })?; + prune_config_backups(backup_dir, &prefix, &suffix); + Ok(()) +} + +/// A test can stop a save after it reads the current config, to make two saves +/// overlap at the point where a lost backup can occur. +#[cfg(test)] +type BackupReadHook = std::sync::Arc; + +#[cfg(test)] +static AFTER_BACKUP_READ_HOOK: std::sync::Mutex> = + std::sync::Mutex::new(None); + +#[cfg(test)] +fn run_after_backup_read_hook(path: &Path) { + let hook = AFTER_BACKUP_READ_HOOK + .lock() + .unwrap_or_else(|poison| poison.into_inner()) + .clone(); + if let Some(hook) = hook { + hook(path); + } +} + +fn read_current_config_no_follow(path: &Path) -> std::io::Result>> { + use std::io::Read; + + let mut options = std::fs::OpenOptions::new(); + options.read(true); + #[cfg(unix)] + { + use std::os::unix::fs::OpenOptionsExt; + options.custom_flags(libc::O_NOFOLLOW); + } + let mut file = match options.open(path) { + Ok(file) => file, + Err(e) if e.kind() == ErrorKind::NotFound => return Ok(None), + Err(e) => return Err(e), + }; + if !file.metadata()?.is_file() { + return Err(std::io::Error::other( + "the config path is not a regular file", + )); + } + let mut bytes = Vec::new(); + file.read_to_end(&mut bytes)?; + Ok(Some(bytes)) +} + +/// Make the backup folder owner-only. Refuse a symlink at the folder path, so +/// that a copy does not go to a folder that a different path selects. +fn ensure_private_backup_directory(dir: &Path) -> std::io::Result<()> { + match std::fs::symlink_metadata(dir) { + Ok(_) => {} + Err(e) if e.kind() == ErrorKind::NotFound => ensure_private_config_directory(dir)?, + Err(e) => return Err(e), + } + let metadata = std::fs::symlink_metadata(dir)?; + if metadata.file_type().is_symlink() { + return Err(std::io::Error::other("the folder is a symlink")); + } + if !metadata.is_dir() { + return Err(std::io::Error::other("the path is not a directory")); + } + #[cfg(unix)] + { + use std::os::unix::fs::{OpenOptionsExt, PermissionsExt}; + // Open the folder with O_NOFOLLOW and change the mode through that + // file descriptor, so that the chmod cannot touch a different folder. + let handle = std::fs::OpenOptions::new() + .read(true) + .custom_flags(libc::O_NOFOLLOW | libc::O_DIRECTORY) + .open(dir)?; + if handle.metadata()?.permissions().mode() & 0o777 != 0o700 { + handle.set_permissions(std::fs::Permissions::from_mode(0o700))?; + } + } + Ok(()) +} + +/// Write one copy as `..-`. The +/// timestamp width does not change, so name order is time order. The counter and +/// `create_new` make sure that two saves at the same time do not write to the +/// same copy. +fn write_backup_copy( + backup_dir: &Path, + prefix: &str, + suffix: &str, + content: &[u8], +) -> std::io::Result<()> { + let stamp = chrono::Utc::now().format("%Y%m%dT%H%M%S%.9fZ").to_string(); + for attempt in 0..100u8 { + let copy_path = backup_dir.join(format!("{prefix}{stamp}-{attempt:02}{suffix}")); + let mut options = std::fs::OpenOptions::new(); + options.write(true).create_new(true); + #[cfg(unix)] + { + use std::os::unix::fs::OpenOptionsExt; + options.custom_flags(libc::O_NOFOLLOW).mode(0o600); + } + let mut file = match options.open(©_path) { + Ok(file) => file, + Err(e) if e.kind() == ErrorKind::AlreadyExists => continue, + Err(e) => return Err(e), + }; + let written = (|| { + #[cfg(unix)] + { + use std::os::unix::fs::PermissionsExt; + file.set_permissions(std::fs::Permissions::from_mode(0o600))?; + } + file.write_all(content)?; + file.sync_all() + })(); + if let Err(e) = written { + let _ = std::fs::remove_file(©_path); + return Err(e); + } + return Ok(()); + } + Err(std::io::Error::other("no unique backup copy name")) +} + +/// Remove all but the newest copies of one config. Only regular files with +/// this config's prefix, suffix and timestamp shape go, so copies of other +/// configs stay. A failure here does not stop the save: the copy of the +/// current file is on disk, and the next save tries again. +fn prune_config_backups(backup_dir: &Path, prefix: &str, suffix: &str) { + let Ok(entries) = std::fs::read_dir(backup_dir) else { + return; + }; + let mut copies: Vec = entries + .filter_map(|entry| entry.ok()) + .filter(|entry| entry.file_type().map(|t| t.is_file()).unwrap_or(false)) + .filter_map(|entry| entry.file_name().to_str().map(str::to_string)) + .filter(|name| { + name.strip_prefix(prefix) + .and_then(|rest| rest.strip_suffix(suffix)) + .is_some_and(is_backup_stamp) + }) + .collect(); + if copies.len() <= CONFIG_BACKUP_LIMIT { + return; + } + copies.sort(); + let excess = copies.len() - CONFIG_BACKUP_LIMIT; + for name in &copies[..excess] { + let _ = std::fs::remove_file(backup_dir.join(name)); + } +} + +/// Agrees with `YYYYMMDDTHHMMSS.NNNNNNNNNZ-NN`, the shape that `write_backup_copy` writes. +fn is_backup_stamp(value: &str) -> bool { + let bytes = value.as_bytes(); + bytes.len() == 29 + && bytes.iter().enumerate().all(|(i, b)| match i { + 8 => *b == b'T', + 15 => *b == b'.', + 25 => *b == b'Z', + 26 => *b == b'-', + _ => b.is_ascii_digit(), + }) +} + fn ensure_private_config_directory(path: &Path) -> std::io::Result<()> { #[cfg(unix)] { @@ -506,6 +892,8 @@ mod tests { let dir = tempfile::tempdir().unwrap(); let mut env = crate::test_env::EnvGuard::new(); env.set("XDG_CONFIG_HOME", dir.path()); + env.set("XDG_DATA_HOME", dir.path().join("data")); + env.remove("MAGIC_CONTEXT_STORAGE_DIR"); let expected = dir.path().join("cortexkit/magic-context.jsonc"); assert_eq!(resolve_pi_config_path(&config_home()), expected); @@ -564,6 +952,7 @@ mod tests { use std::os::unix::fs::symlink; let dir = tempfile::tempdir().unwrap(); + let (_env, _) = isolated_data_home(dir.path()); let dotfiles = dir.path().join("dotfiles/cortexkit"); std::fs::create_dir_all(&dotfiles).unwrap(); let real = dotfiles.join("magic-context.jsonc"); @@ -608,6 +997,7 @@ mod tests { )); let mut builder = std::fs::DirBuilder::new(); builder.recursive(true).mode(0o700).create(&root).unwrap(); + let (_env, _) = isolated_data_home(&root); let path = root.join("config/cortexkit/magic-context.jsonc"); write_config(&path, "{ \"api_key\": \"secret\" }\n").expect("write"); @@ -633,6 +1023,7 @@ mod tests { use std::os::unix::fs::PermissionsExt; let dir = tempfile::tempdir().unwrap(); + let (_env, _) = isolated_data_home(dir.path()); let path = dir.path().join("magic-context.jsonc"); std::fs::write(&path, "{}\n").unwrap(); std::fs::set_permissions(&path, std::fs::Permissions::from_mode(0o600)).unwrap(); @@ -660,6 +1051,7 @@ mod tests { #[test] fn write_project_config_writes_regular_file_atomically() { let dir = tempfile::tempdir().unwrap(); + let (_env, _) = isolated_data_home(dir.path()); let project = dir.path().join("project"); std::fs::create_dir(&project).unwrap(); let canonical_project = project.canonicalize().unwrap(); @@ -692,6 +1084,7 @@ mod tests { use std::os::unix::fs::symlink; let dir = tempfile::tempdir().unwrap(); + let (_env, _) = isolated_data_home(dir.path()); let project = dir.path().join("project"); let workspace_member = dir.path().join("workspace/member"); std::fs::create_dir(&project).unwrap(); @@ -750,6 +1143,433 @@ mod tests { assert!(err.contains("regular file"), "unexpected error: {err}"); } + // ── config backup tests ───────────────────────────────────────── + + /// Point the backup store at a temporary data home. The guard also holds + /// the environment lock, so parallel tests cannot see a different value. + fn isolated_data_home(root: &Path) -> (crate::test_env::EnvGuard, PathBuf) { + let mut env = crate::test_env::EnvGuard::new(); + let data_home = root.join("data"); + env.set("XDG_DATA_HOME", &data_home); + env.remove("MAGIC_CONTEXT_STORAGE_DIR"); + ( + env, + data_home.join("cortexkit/magic-context/config-backups"), + ) + } + + fn sorted_names(dir: &Path) -> Vec { + let mut names: Vec = match std::fs::read_dir(dir) { + Ok(entries) => entries + .map(|entry| entry.unwrap().file_name().to_string_lossy().to_string()) + .collect(), + Err(_) => Vec::new(), + }; + names.sort(); + names + } + + /// The config copies in a backup folder, without the lock files. + fn backup_copies(dir: &Path) -> Vec { + sorted_names(dir) + .into_iter() + .filter(|name| !name.ends_with(".lock")) + .collect() + } + + /// Remove the read hook also when the test fails. + struct BackupReadHookGuard; + + impl BackupReadHookGuard { + fn set(hook: BackupReadHook) -> Self { + *AFTER_BACKUP_READ_HOOK + .lock() + .unwrap_or_else(|poison| poison.into_inner()) = Some(hook); + Self + } + } + + impl Drop for BackupReadHookGuard { + fn drop(&mut self) { + *AFTER_BACKUP_READ_HOOK + .lock() + .unwrap_or_else(|poison| poison.into_inner()) = None; + } + } + + fn user_config_in(root: &Path, content: &str) -> PathBuf { + let path = root.join("config/cortexkit/magic-context.jsonc"); + std::fs::create_dir_all(path.parent().unwrap()).unwrap(); + std::fs::write(&path, content).unwrap(); + path + } + + #[test] + fn save_keeps_the_previous_bytes_in_the_data_folder() { + let dir = tempfile::tempdir().unwrap(); + let (_env, backups) = isolated_data_home(dir.path()); + let path = user_config_in(dir.path(), "{ \"version\": 0 }\n"); + + write_config(&path, "{ \"version\": 1 }\n").expect("save"); + + assert_eq!( + std::fs::read_to_string(&path).unwrap(), + "{ \"version\": 1 }\n" + ); + let names = backup_copies(&backups); + assert_eq!(names.len(), 1, "copies: {names:?}"); + assert!( + names[0].starts_with("magic-context.user.") && names[0].ends_with(".jsonc"), + "unexpected copy name: {}", + names[0] + ); + assert_eq!( + std::fs::read_to_string(backups.join(&names[0])).unwrap(), + "{ \"version\": 0 }\n" + ); + // The config folder gets no more files. + assert_eq!( + sorted_names(path.parent().unwrap()), + vec!["magic-context.jsonc".to_string()] + ); + } + + #[test] + fn first_save_of_a_new_config_makes_no_copy() { + let dir = tempfile::tempdir().unwrap(); + let (_env, backups) = isolated_data_home(dir.path()); + let path = dir.path().join("config/cortexkit/magic-context.jsonc"); + + write_config(&path, "{}\n").expect("save"); + + assert_eq!(std::fs::read_to_string(&path).unwrap(), "{}\n"); + assert!(backup_copies(&backups).is_empty()); + } + + #[test] + fn sixth_save_drops_the_oldest_copy_only() { + let dir = tempfile::tempdir().unwrap(); + let (_env, backups) = isolated_data_home(dir.path()); + let path = user_config_in(dir.path(), "v0"); + // A copy of a different config must stay after the pruning, also when + // it is older than all copies of this config. + std::fs::create_dir_all(&backups).unwrap(); + let other = "magic-context.project-0123456789ab.20000101T000000.000000000Z-00.jsonc"; + std::fs::write(backups.join(other), "other").unwrap(); + + // The saves run in a fast loop, so some occur in one second: a copy + // must not replace an earlier copy. + for version in 1..=6 { + write_config(&path, &format!("v{version}")).expect("save"); + } + + assert_eq!(std::fs::read_to_string(&path).unwrap(), "v6"); + let names = sorted_names(&backups); + assert!(names.contains(&other.to_string()), "copies: {names:?}"); + let contents: Vec = names + .iter() + .filter(|name| name.starts_with("magic-context.user.")) + .map(|name| std::fs::read_to_string(backups.join(name)).unwrap()) + .collect(); + assert_eq!(contents, vec!["v1", "v2", "v3", "v4", "v5"]); + } + + #[test] + fn copy_failure_refuses_the_save_and_leaves_the_config_untouched() { + let dir = tempfile::tempdir().unwrap(); + let (_env, backups) = isolated_data_home(dir.path()); + let path = user_config_in(dir.path(), "{ \"keep\": true }\n"); + // A regular file where the backup folder must be: no copy can be made. + std::fs::create_dir_all(backups.parent().unwrap()).unwrap(); + std::fs::write(&backups, "not a directory").unwrap(); + + let err = write_config(&path, "{ \"keep\": false }\n") + .expect_err("save without a copy must be refused"); + + assert!(err.contains("backup"), "unexpected error: {err}"); + assert_eq!( + std::fs::read_to_string(&path).unwrap(), + "{ \"keep\": true }\n" + ); + assert_eq!( + sorted_names(path.parent().unwrap()), + vec!["magic-context.jsonc".to_string()] + ); + } + + #[test] + fn copies_go_to_the_storage_override_when_it_is_set() { + let dir = tempfile::tempdir().unwrap(); + let (mut env, xdg_backups) = isolated_data_home(dir.path()); + let storage = dir.path().join("store"); + env.set("MAGIC_CONTEXT_STORAGE_DIR", &storage); + let path = user_config_in(dir.path(), "v0"); + + write_config(&path, "v1").expect("save"); + + assert_eq!(backup_copies(&storage.join("config-backups")).len(), 1); + assert!(sorted_names(&xdg_backups).is_empty()); + } + + #[test] + fn project_copy_lands_in_the_data_folder_with_a_project_hash() { + use sha2::{Digest, Sha256}; + + let dir = tempfile::tempdir().unwrap(); + let (_env, backups) = isolated_data_home(dir.path()); + let project = dir.path().join("project"); + std::fs::create_dir_all(project.join(".cortexkit")).unwrap(); + let canonical_project = project.canonicalize().unwrap(); + let config = canonical_project.join(".cortexkit/magic-context.jsonc"); + std::fs::write(&config, "p0").unwrap(); + + write_project_config(canonical_project.to_str().unwrap(), "p1").expect("save"); + + assert_eq!(std::fs::read_to_string(&config).unwrap(), "p1"); + assert_eq!( + sorted_names(&canonical_project.join(".cortexkit")), + vec!["magic-context.jsonc".to_string()] + ); + let digest = Sha256::digest(canonical_project.to_string_lossy().as_bytes()); + let hash: String = digest.iter().map(|b| format!("{b:02x}")).collect(); + let prefix = format!("magic-context.project-{}.", &hash[..12]); + let names = backup_copies(&backups); + assert_eq!(names.len(), 1, "copies: {names:?}"); + assert!( + names[0].starts_with(&prefix), + "unexpected name: {}", + names[0] + ); + assert_eq!( + std::fs::read_to_string(backups.join(&names[0])).unwrap(), + "p0" + ); + } + + #[cfg(unix)] + #[test] + fn backup_folder_and_copies_are_owner_only() { + use std::os::unix::fs::PermissionsExt; + + let dir = tempfile::tempdir().unwrap(); + let (_env, backups) = isolated_data_home(dir.path()); + // The save narrows the mode of a folder that has a wide mode. + std::fs::create_dir_all(&backups).unwrap(); + std::fs::set_permissions(&backups, std::fs::Permissions::from_mode(0o755)).unwrap(); + let path = user_config_in(dir.path(), "{ \"api_key\": \"secret\" }\n"); + std::fs::set_permissions(&path, std::fs::Permissions::from_mode(0o644)).unwrap(); + + write_config(&path, "{}\n").expect("save"); + + let mode = |p: &Path| std::fs::metadata(p).unwrap().permissions().mode() & 0o777; + assert_eq!(mode(&backups), 0o700); + let names = backup_copies(&backups); + assert_eq!(names.len(), 1, "copies: {names:?}"); + assert_eq!(mode(&backups.join(&names[0])), 0o600); + } + + #[cfg(unix)] + #[test] + fn symlinked_backup_folder_refuses_the_save() { + use std::os::unix::fs::symlink; + + let dir = tempfile::tempdir().unwrap(); + let (_env, backups) = isolated_data_home(dir.path()); + let elsewhere = dir.path().join("elsewhere"); + std::fs::create_dir_all(&elsewhere).unwrap(); + std::fs::create_dir_all(backups.parent().unwrap()).unwrap(); + symlink(&elsewhere, &backups).unwrap(); + let path = user_config_in(dir.path(), "v0"); + + let err = write_config(&path, "v1").expect_err("symlinked folder must be refused"); + + assert!(err.contains("backup"), "unexpected error: {err}"); + assert_eq!(std::fs::read_to_string(&path).unwrap(), "v0"); + assert!(sorted_names(&elsewhere).is_empty()); + } + + /// Two saves that overlap must not both copy the same old bytes: the + /// save that runs second must copy what the first save wrote. + #[test] + fn overlapping_saves_back_up_each_replaced_version() { + use std::sync::{Arc, Condvar, Mutex}; + + let dir = tempfile::tempdir().unwrap(); + let (_env, backups) = isolated_data_home(dir.path()); + let path = user_config_in(dir.path(), "A"); + // After it reads the current config, each save waits until the other + // save also read it, or until the timeout. Without serialization, both + // saves read "A". With serialization, the second save cannot read + // before the first save replaced the file, so the first wait ends at + // the timeout and the result does not depend on thread timing. + let arrivals = Arc::new((Mutex::new(0usize), Condvar::new())); + let hook_arrivals = Arc::clone(&arrivals); + let hook_path = path.clone(); + let _hook = BackupReadHookGuard::set(Arc::new(move |read_path: &Path| { + if read_path != hook_path { + return; + } + let (count, ready) = &*hook_arrivals; + let mut count = count.lock().unwrap(); + *count += 1; + ready.notify_all(); + let _ = ready + .wait_timeout_while(count, std::time::Duration::from_secs(2), |count| *count < 2) + .unwrap(); + })); + + let savers: Vec<_> = ["B", "C"] + .into_iter() + .map(|content| { + let path = path.clone(); + std::thread::spawn(move || write_config(&path, content)) + }) + .collect(); + for saver in savers { + saver.join().unwrap().expect("save"); + } + + let last = std::fs::read_to_string(&path).unwrap(); + let first = if last == "B" { "C" } else { "B" }; + let contents: Vec = backup_copies(&backups) + .iter() + .map(|name| std::fs::read_to_string(backups.join(name)).unwrap()) + .collect(); + assert_eq!(contents, vec!["A".to_string(), first.to_string()]); + } + + /// Open the lock file of a backup scope and lock it, as a save in a + /// different dashboard process does. + #[cfg(unix)] + fn hold_scope_lock(backups: &Path, scope: &str) -> std::fs::File { + use std::os::unix::io::AsRawFd; + + ensure_private_backup_directory(backups).unwrap(); + let file = std::fs::OpenOptions::new() + .read(true) + .write(true) + .create(true) + .truncate(false) + .open(backups.join(format!(".{scope}.lock"))) + .unwrap(); + assert_eq!( + unsafe { libc::flock(file.as_raw_fd(), libc::LOCK_EX | libc::LOCK_NB) }, + 0 + ); + file + } + + /// A save waits while a different process holds the lock, and then copies + /// what that process wrote. + #[cfg(unix)] + #[test] + fn save_waits_for_a_scope_lock_held_by_another_handle() { + let dir = tempfile::tempdir().unwrap(); + let (_env, backups) = isolated_data_home(dir.path()); + let path = user_config_in(dir.path(), "A"); + let holder = hold_scope_lock(&backups, "user"); + + let saver = { + let path = path.clone(); + std::thread::spawn(move || write_config(&path, "B")) + }; + // A save that does not wait finishes in this time and copies "A". + std::thread::sleep(std::time::Duration::from_millis(300)); + std::fs::write(&path, "X").unwrap(); + drop(holder); + saver.join().unwrap().expect("save"); + + assert_eq!(std::fs::read_to_string(&path).unwrap(), "B"); + let contents: Vec = backup_copies(&backups) + .iter() + .map(|name| std::fs::read_to_string(backups.join(name)).unwrap()) + .collect(); + assert_eq!(contents, vec!["X".to_string()]); + } + + /// Copies of a symlinked user config get the name of the config, not of + /// the file that the link points at, so that one set of copies is pruned + /// also after the link changes to a different file. + #[cfg(unix)] + #[test] + fn symlinked_user_config_copies_use_the_config_name() { + use std::os::unix::fs::symlink; + + let dir = tempfile::tempdir().unwrap(); + let (_env, backups) = isolated_data_home(dir.path()); + let dotfiles = dir.path().join("dotfiles"); + std::fs::create_dir_all(&dotfiles).unwrap(); + std::fs::write(dotfiles.join("other-name.jsonc"), "v0").unwrap(); + std::fs::write(dotfiles.join("third-name.jsonc"), "w0").unwrap(); + let config_dir = dir.path().join("config/cortexkit"); + std::fs::create_dir_all(&config_dir).unwrap(); + let link = config_dir.join("magic-context.jsonc"); + symlink(dotfiles.join("other-name.jsonc"), &link).unwrap(); + + for version in 1..=3 { + write_config(&link, &format!("v{version}")).expect("save"); + } + std::fs::remove_file(&link).unwrap(); + symlink(dotfiles.join("third-name.jsonc"), &link).unwrap(); + for version in 1..=3 { + write_config(&link, &format!("w{version}")).expect("save"); + } + + let names = backup_copies(&backups); + assert!( + names + .iter() + .all(|name| name.starts_with("magic-context.user.") && name.ends_with(".jsonc")), + "copies: {names:?}" + ); + let contents: Vec = names + .iter() + .map(|name| std::fs::read_to_string(backups.join(name)).unwrap()) + .collect(); + assert_eq!(contents, vec!["v1", "v2", "w0", "w1", "w2"]); + } + + /// If a different process holds the lock for longer than the wait, the + /// save is refused. + #[cfg(unix)] + #[test] + fn scope_lock_held_past_the_wait_is_refused() { + let dir = tempfile::tempdir().unwrap(); + let backups = dir.path().join("config-backups"); + let _holder = hold_scope_lock(&backups, "user"); + + let err = lock_backup_scope(&backups, "user", std::time::Duration::ZERO) + .expect_err("a held lock must refuse"); + + assert!(err.contains("cannot lock the backup folder"), "{err}"); + // A different backup set does not wait for this lock. + assert!( + lock_backup_scope(&backups, "project-0123456789ab", std::time::Duration::ZERO).is_ok() + ); + } + + /// A symlink at the lock path refuses the save and does not touch the file + /// that the link points at. + #[cfg(unix)] + #[test] + fn symlinked_lock_file_refuses_the_save() { + use std::os::unix::fs::symlink; + + let dir = tempfile::tempdir().unwrap(); + let (_env, backups) = isolated_data_home(dir.path()); + ensure_private_backup_directory(&backups).unwrap(); + let elsewhere = dir.path().join("elsewhere.txt"); + std::fs::write(&elsewhere, "keep").unwrap(); + symlink(&elsewhere, backups.join(".user.lock")).unwrap(); + let path = user_config_in(dir.path(), "v0"); + + let err = write_config(&path, "v1").expect_err("symlinked lock must be refused"); + + assert!(err.contains("cannot lock the backup folder"), "{err}"); + assert_eq!(std::fs::read_to_string(&path).unwrap(), "v0"); + assert_eq!(std::fs::read_to_string(&elsewhere).unwrap(), "keep"); + } + // ── discover_project_configs_with_db tests ───────────────────── /// Create a minimal opencode.db with a project table.