Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 1 addition & 9 deletions src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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())
}
}

Expand Down
65 changes: 64 additions & 1 deletion src/paths.rs
Original file line number Diff line number Diff line change
@@ -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<PathBuf> {
xdg_path("XDG_CONFIG_HOME", ".config").map(|path| path.join("whispers"))
}
Expand Down Expand Up @@ -81,3 +112,35 @@ fn xdg_path(variable: &str, fallback: &str) -> Result<PathBuf> {
.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);
}
}
16 changes: 2 additions & 14 deletions src/vocabulary.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use std::fs;
use std::path::{Path, PathBuf};
use std::path::Path;

use serde::{Deserialize, Serialize};

Expand Down Expand Up @@ -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<String>) -> Result<()> {
Expand Down Expand Up @@ -185,12 +179,6 @@ 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::*;
Expand Down
Loading