From 18c59e869a7eb58d175eba600f7ef71ff52d6336 Mon Sep 17 00:00:00 2001 From: xqqmdy Date: Sat, 14 Nov 2026 18:51:09 +0800 Subject: [PATCH 1/2] fix: keep symlinked configuration and vocabulary in place --- src/config.rs | 28 +++++++++++++++++++--------- src/paths.rs | 33 ++++++++++++++++++++++++++++++++- src/vocabulary.rs | 36 ++++++++++++++++++++++-------------- 3 files changed, 73 insertions(+), 24 deletions(-) diff --git a/src/config.rs b/src/config.rs index 676387b..0bf9b3b 100644 --- a/src/config.rs +++ b/src/config.rs @@ -127,15 +127,7 @@ impl Config { } pub fn save_to(&self, path: &Path) -> Result<()> { - if let Some(parent) = path.parent() { - fs::create_dir_all(parent)?; - } - let mut temporary = path.as_os_str().to_os_string(); - temporary.push(format!(".{}.tmp", std::process::id())); - let temporary = PathBuf::from(temporary); - fs::write(&temporary, toml::to_string_pretty(self)?)?; - fs::rename(temporary, path)?; - Ok(()) + paths::write_atomic(path, toml::to_string_pretty(self)?.as_bytes()) } } @@ -143,6 +135,24 @@ impl Config { mod tests { use super::*; + #[test] + fn saving_through_a_symlink_updates_the_link_target() { + let directory = + std::env::temp_dir().join(format!("whispers-config-symlink-{}", std::process::id())); + let _ = fs::remove_dir_all(&directory); + assert!(fs::create_dir_all(&directory).is_ok()); + let target = directory.join("dotfiles-config.toml"); + let link = directory.join("config.toml"); + assert!(fs::write(&target, "version = 1\n").is_ok()); + assert!(std::os::unix::fs::symlink(&target, &link).is_ok()); + + assert!(Config::default().save_to(&link).is_ok()); + + assert!(link.is_symlink()); + assert!(fs::read_to_string(&target).is_ok_and(|text| text.contains("version = 2"))); + let _ = fs::remove_dir_all(&directory); + } + #[test] fn existing_unversioned_configuration_requires_migration() { let path = std::env::temp_dir().join(format!( diff --git a/src/paths.rs b/src/paths.rs index b8e845f..bb4c98e 100644 --- a/src/paths.rs +++ b/src/paths.rs @@ -1,10 +1,41 @@ use std::env; -use std::path::PathBuf; +use std::fs; +use std::path::{Path, PathBuf}; use crate::{Error, Result}; pub const MANAGED_LLAMA_RUNTIME: &str = "runtimes/llama.cpp/b10258-vulkan"; +/// Writes `contents` to `path` through an atomic rename, following symbolic +/// links so that dotfile-managed files stay links to their real location. +pub(crate) fn write_atomic(path: &Path, contents: &[u8]) -> Result<()> { + let target = resolve_symlink(path); + if let Some(parent) = target.parent() { + fs::create_dir_all(parent)?; + } + let mut temporary = target.as_os_str().to_os_string(); + temporary.push(format!(".{}.tmp", std::process::id())); + let temporary = PathBuf::from(temporary); + fs::write(&temporary, contents)?; + fs::rename(&temporary, &target)?; + Ok(()) +} + +fn resolve_symlink(path: &Path) -> PathBuf { + if !path.is_symlink() { + return path.to_path_buf(); + } + if let Ok(canonical) = fs::canonicalize(path) { + return canonical; + } + let parent = path.parent().unwrap_or_else(|| Path::new(".")); + match fs::read_link(path) { + Ok(link) if link.is_absolute() => link, + Ok(link) => parent.join(link), + Err(_) => path.to_path_buf(), + } +} + pub fn config_dir() -> Result { xdg_path("XDG_CONFIG_HOME", ".config").map(|path| path.join("whispers")) } diff --git a/src/vocabulary.rs b/src/vocabulary.rs index 56d76a6..90eda61 100644 --- a/src/vocabulary.rs +++ b/src/vocabulary.rs @@ -1,5 +1,5 @@ use std::fs; -use std::path::{Path, PathBuf}; +use std::path::Path; use serde::{Deserialize, Serialize}; @@ -57,13 +57,7 @@ impl Vocabulary { } pub fn save_to(&self, path: &Path) -> Result<()> { - if let Some(parent) = path.parent() { - fs::create_dir_all(parent)?; - } - let temporary = temporary_path(path); - fs::write(&temporary, toml::to_string_pretty(self)?)?; - fs::rename(temporary, path)?; - Ok(()) + paths::write_atomic(path, toml::to_string_pretty(self)?.as_bytes()) } pub fn add(&mut self, term: &str, aliases: Vec) -> Result<()> { @@ -185,16 +179,30 @@ fn word_character(character: char) -> bool { character.is_alphanumeric() || character == '_' } -fn temporary_path(path: &Path) -> PathBuf { - let mut temporary = path.as_os_str().to_os_string(); - temporary.push(format!(".{}.tmp", std::process::id())); - PathBuf::from(temporary) -} - #[cfg(test)] mod tests { use super::*; + #[test] + fn saving_through_a_symlink_updates_the_link_target() { + let directory = std::env::temp_dir().join(format!( + "whispers-vocabulary-symlink-{}", + std::process::id() + )); + let _ = fs::remove_dir_all(&directory); + assert!(fs::create_dir_all(&directory).is_ok()); + let target = directory.join("dotfiles-vocabulary.toml"); + let link = directory.join("vocabulary.toml"); + assert!(fs::write(&target, "version = 0\n").is_ok()); + assert!(std::os::unix::fs::symlink(&target, &link).is_ok()); + + assert!(Vocabulary::default().save_to(&link).is_ok()); + + assert!(link.is_symlink()); + assert!(fs::read_to_string(&target).is_ok_and(|text| text.contains("version = 1"))); + let _ = fs::remove_dir_all(&directory); + } + #[test] fn replaces_aliases_at_word_boundaries() { let vocabulary = Vocabulary { From 0f6d4ea40e529b430b6e9a9b49a78100e6a758e2 Mon Sep 17 00:00:00 2001 From: Jonatan Jonasson Date: Sun, 15 Nov 2026 22:45:17 +0100 Subject: [PATCH 2/2] test: cover symlinked writes once in paths --- src/config.rs | 18 ------------------ src/paths.rs | 32 ++++++++++++++++++++++++++++++++ src/vocabulary.rs | 20 -------------------- 3 files changed, 32 insertions(+), 38 deletions(-) diff --git a/src/config.rs b/src/config.rs index 0bf9b3b..e6c1062 100644 --- a/src/config.rs +++ b/src/config.rs @@ -135,24 +135,6 @@ impl Config { mod tests { use super::*; - #[test] - fn saving_through_a_symlink_updates_the_link_target() { - let directory = - std::env::temp_dir().join(format!("whispers-config-symlink-{}", std::process::id())); - let _ = fs::remove_dir_all(&directory); - assert!(fs::create_dir_all(&directory).is_ok()); - let target = directory.join("dotfiles-config.toml"); - let link = directory.join("config.toml"); - assert!(fs::write(&target, "version = 1\n").is_ok()); - assert!(std::os::unix::fs::symlink(&target, &link).is_ok()); - - assert!(Config::default().save_to(&link).is_ok()); - - assert!(link.is_symlink()); - assert!(fs::read_to_string(&target).is_ok_and(|text| text.contains("version = 2"))); - let _ = fs::remove_dir_all(&directory); - } - #[test] fn existing_unversioned_configuration_requires_migration() { let path = std::env::temp_dir().join(format!( diff --git a/src/paths.rs b/src/paths.rs index bb4c98e..7c7dd7d 100644 --- a/src/paths.rs +++ b/src/paths.rs @@ -112,3 +112,35 @@ fn xdg_path(variable: &str, fallback: &str) -> Result { .map(|path| path.join(fallback)) .ok_or_else(|| Error::message(format!("neither {variable} nor HOME is set"))) } + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn atomic_writes_follow_symlinks() { + let directory = + env::temp_dir().join(format!("whispers-write-atomic-{}", std::process::id())); + let _ = fs::remove_dir_all(&directory); + assert!(fs::create_dir_all(&directory).is_ok()); + let target = directory.join("target.toml"); + let link = directory.join("link.toml"); + let dangling = directory.join("dangling.toml"); + assert!(fs::write(&target, "old").is_ok()); + assert!(std::os::unix::fs::symlink(&target, &link).is_ok()); + assert!(std::os::unix::fs::symlink("missing.toml", &dangling).is_ok()); + + assert!(write_atomic(&link, b"new").is_ok()); + assert!(write_atomic(&dangling, b"created").is_ok()); + + assert!(link.is_symlink() && dangling.is_symlink()); + assert_eq!(fs::read_to_string(&target).ok().as_deref(), Some("new")); + assert_eq!( + fs::read_to_string(directory.join("missing.toml")) + .ok() + .as_deref(), + Some("created") + ); + let _ = fs::remove_dir_all(&directory); + } +} diff --git a/src/vocabulary.rs b/src/vocabulary.rs index 90eda61..ede896e 100644 --- a/src/vocabulary.rs +++ b/src/vocabulary.rs @@ -183,26 +183,6 @@ fn word_character(character: char) -> bool { mod tests { use super::*; - #[test] - fn saving_through_a_symlink_updates_the_link_target() { - let directory = std::env::temp_dir().join(format!( - "whispers-vocabulary-symlink-{}", - std::process::id() - )); - let _ = fs::remove_dir_all(&directory); - assert!(fs::create_dir_all(&directory).is_ok()); - let target = directory.join("dotfiles-vocabulary.toml"); - let link = directory.join("vocabulary.toml"); - assert!(fs::write(&target, "version = 0\n").is_ok()); - assert!(std::os::unix::fs::symlink(&target, &link).is_ok()); - - assert!(Vocabulary::default().save_to(&link).is_ok()); - - assert!(link.is_symlink()); - assert!(fs::read_to_string(&target).is_ok_and(|text| text.contains("version = 1"))); - let _ = fs::remove_dir_all(&directory); - } - #[test] fn replaces_aliases_at_word_boundaries() { let vocabulary = Vocabulary {