From 15ca363de5aaf8edc5ddedeb76fd89a09fbd5ce3 Mon Sep 17 00:00:00 2001 From: Lorenzo Date: Sun, 1 Nov 2026 10:39:54 -0300 Subject: [PATCH 1/6] fix: wt does not write into the temporary file of another process The pid in the name of the temporary file is unique only in one PID namespace on one host. A devcontainer that mounts the home directory, or a home directory on NFS, can run two wt with the same pid. fs::write emptied the file of the other process and wrote into it, so the rename could put part of a file in the place of the config. When a step failed, wt also removed the file of the other process. wt now makes the temporary file with File::create_new. A file that is present stops the write, and wt removes the temporary file only when it made that file. The name ends in .tmp, so a file that a crash leaves says what it is. --- src/config.rs | 51 +++++++++++++++++++++++++++++++++++++++++++-------- 1 file changed, 43 insertions(+), 8 deletions(-) diff --git a/src/config.rs b/src/config.rs index bf14b39..729161b 100644 --- a/src/config.rs +++ b/src/config.rs @@ -3,6 +3,7 @@ use serde::{Deserialize, Serialize}; use std::cmp::Ordering; use std::collections::{BTreeMap, BTreeSet}; use std::fmt; +use std::io::Write; use std::os::unix::fs::MetadataExt; use std::path::{Path, PathBuf}; @@ -285,18 +286,20 @@ impl Config { if old.as_ref().is_some_and(|md| md.nlink() > 1) { return in_place(); } - // The process id gives each wt that writes a different name. + // The process id gives each wt that writes a different name. Two + // processes in two PID namespaces, or on two hosts of one home + // directory, can have the same pid. The second one then finds the + // file present, and it stops: it does not write into the file or + // remove it. let name = path .file_name() .map(|n| n.to_string_lossy().into_owned()) .unwrap_or_default(); - let tmp = path.with_file_name(format!(".{name}.{}", std::process::id())); - let written = std::fs::write(&tmp, &body) - .and_then(|()| match &old { - Some(md) => std::fs::set_permissions(&tmp, md.permissions()), - None => Ok(()), - }) - .and_then(|()| std::fs::rename(&tmp, &path)); + let tmp = path.with_file_name(format!(".{name}.{}.tmp", std::process::id())); + let file = std::fs::File::create_new(&tmp) + .with_context(|| format!("wt cannot write {}", tmp.display()))?; + let written = + write_new(file, old.as_ref(), &body).and_then(|()| std::fs::rename(&tmp, &path)); match written { Ok(()) => Ok(()), Err(e) => { @@ -318,6 +321,19 @@ impl Config { } } +/// Write `body` into the new file `file`, with the mode of the old file. +fn write_new( + mut file: std::fs::File, + old: Option<&std::fs::Metadata>, + body: &str, +) -> std::io::Result<()> { + file.write_all(body.as_bytes())?; + if let Some(old) = old { + file.set_permissions(old.permissions())?; + } + Ok(()) +} + /// The file at the end of the links from `path`. /// /// The file does not need to exist: a link into the dotfiles can point to a @@ -676,6 +692,25 @@ mod tests { assert!(std::fs::read_to_string(&dotfiles).unwrap().contains(".env")); } + /// A wt with the same pid, in a different PID namespace or on a + /// different host of one home directory, writes the temporary file. + #[test] + fn the_temporary_file_of_another_process_stays_as_it_is() { + let tmp = tempfile::TempDir::new().unwrap(); + let path = tmp.path().join("config.toml"); + std::fs::write(&path, "old").unwrap(); + let other = tmp + .path() + .join(format!(".config.toml.{}.tmp", std::process::id())); + std::fs::write(&other, "other").unwrap(); + + let error = one_path().write_to(&path).unwrap_err().to_string(); + + assert!(error.contains("wt cannot write"), "{error}"); + assert_eq!(std::fs::read_to_string(&other).unwrap(), "other"); + assert_eq!(std::fs::read_to_string(&path).unwrap(), "old"); + } + /// Each path that `wt share` gets can fail, and the entry then has no /// path. #[test] From d24ad3dc0567636b65bd3d3565b1aee36a4e9204 Mon Sep 17 00:00:00 2001 From: Lorenzo Date: Sun, 1 Nov 2026 10:40:02 -0300 Subject: [PATCH 2/6] fix: the config is on the disk before the rename wt renamed the temporary file over the config, but it did not sync the file first. The system can write the rename to the disk before the body, for example on XFS, so a crash of the system could leave an empty config, and each command and the hook then refused it. wt now syncs the temporary file before the rename. --- src/config.rs | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/config.rs b/src/config.rs index 729161b..c584eb5 100644 --- a/src/config.rs +++ b/src/config.rs @@ -331,7 +331,9 @@ fn write_new( if let Some(old) = old { file.set_permissions(old.permissions())?; } - Ok(()) + // The body must be on the disk before the rename. Else a crash of the + // system can leave an empty file in the place of the config. + file.sync_all() } /// The file at the end of the links from `path`. From 8568a50cb04edfde43ea97411cd5ea623df44c7f Mon Sep 17 00:00:00 2001 From: Lorenzo Date: Sun, 1 Nov 2026 10:40:17 -0300 Subject: [PATCH 3/6] fix: the temporary file gets the owner and the mode before the body fs::write wrote into the old file, so the file kept its owner and its group. A rename puts a new file in its place, and the writer owns that file. After sudo wt share with HOME kept, root owned the config, and with mode 600 each later wt failed to read it. wt also gave the temporary file the mode of the old file only after it wrote the body. Until then, the file had the mode of a new file, 644 with the usual umask, so each user could read the body of a config with mode 600. wt now gives the new file the owner, the group and the mode of the old file first, and it writes the body after them. Root can always give the file back. A user can give the file only to a group of that user, and else the file stays the user's, as any new file does. --- src/config.rs | 26 ++++++++++++++++++++++---- 1 file changed, 22 insertions(+), 4 deletions(-) diff --git a/src/config.rs b/src/config.rs index c584eb5..9a751a8 100644 --- a/src/config.rs +++ b/src/config.rs @@ -4,7 +4,7 @@ use std::cmp::Ordering; use std::collections::{BTreeMap, BTreeSet}; use std::fmt; use std::io::Write; -use std::os::unix::fs::MetadataExt; +use std::os::unix::fs::{MetadataExt, fchown}; use std::path::{Path, PathBuf}; /// The list of shared paths for one repository. @@ -259,7 +259,8 @@ impl Config { /// Write the file at `path` in one step. A reader, for example the hook /// in a different terminal, then reads the old file or the new file, and - /// never a part of one. The new file gets the mode of the old file. + /// never a part of one. The new file gets the owner, the group and the + /// mode of the old file. /// /// The file can be a link into the dotfiles. A rename over the link /// would put a plain file in its place, so the function writes the @@ -321,16 +322,33 @@ impl Config { } } -/// Write `body` into the new file `file`, with the mode of the old file. +/// Write `body` into the new file `file`, with the owner, the group and the +/// mode of the old file. fn write_new( mut file: std::fs::File, old: Option<&std::fs::Metadata>, body: &str, ) -> std::io::Result<()> { - file.write_all(body.as_bytes())?; + // The owner and the mode come before the body. Else each user can read + // the body of a config with mode 600 until the mode changes. if let Some(old) = old { + // Keep the owner of the old file, as a write into it does. The + // rename puts a new file in its place, and the writer owns that + // file: after `sudo wt share`, root would own the config. Root can + // give the file back. A user can give it only to a group of that + // user, and else the file stays the writer's. + let new = file.metadata()?; + if (old.uid(), old.gid()) != (new.uid(), new.gid()) { + match fchown(&file, Some(old.uid()), Some(old.gid())) { + Err(e) if e.kind() == std::io::ErrorKind::PermissionDenied => {} + other => other?, + } + } + // Keep the mode of the old file too. The owner comes first, because + // a change of the owner can clear bits of the mode. file.set_permissions(old.permissions())?; } + file.write_all(body.as_bytes())?; // The body must be on the disk before the rename. Else a crash of the // system can leave an empty file in the place of the config. file.sync_all() From 4b0e2ab2528b3547ca0ea1d658b24a2b5f1896f8 Mon Sep 17 00:00:00 2001 From: Lorenzo Date: Sun, 1 Nov 2026 10:40:44 -0300 Subject: [PATCH 4/6] fix: wt follows a chain of 40 links, as Linux does link_target followed 40 links, but it did not look at the target of the last one, so it refused a chain of 40 links. The kernel follows 40, so wt read such a config and then refused to write it, and the message said "a loop of links". link_target now also looks at the end of the 40th link, and it refuses the 41st. The message says "a chain of more than 40 links", and it names the config, not the last link. The test reads the file at the end of the chain: macOS follows at most 32 links. --- src/config.rs | 57 +++++++++++++++++++++++++++++++++++++++++++++------ 1 file changed, 51 insertions(+), 6 deletions(-) diff --git a/src/config.rs b/src/config.rs index 9a751a8..50e5e91 100644 --- a/src/config.rs +++ b/src/config.rs @@ -354,15 +354,19 @@ fn write_new( file.sync_all() } -/// The file at the end of the links from `path`. +/// Linux follows at most 40 links in a path. +const MAX_LINKS: usize = 40; + +/// The file at the end of the links from `link`. /// /// The file does not need to exist: a link into the dotfiles can point to a /// file that the dotfiles do not have yet. `canonicalize` fails on such a /// link. -fn link_target(path: &Path) -> Result { - let mut path = path.to_path_buf(); - // The system also follows at most 40 links. - for _ in 0..40 { +fn link_target(link: &Path) -> Result { + let mut path = link.to_path_buf(); + // The loop also looks at the end of the 40th link, so it follows 40 + // links, as the system does, and it refuses the 41st. + for _ in 0..=MAX_LINKS { match std::fs::symlink_metadata(&path) { Ok(md) if md.file_type().is_symlink() => { let next = std::fs::read_link(&path) @@ -376,7 +380,10 @@ fn link_target(path: &Path) -> Result { _ => return Ok(path), } } - bail!("{} is a loop of links", path.display()) + bail!( + "{} is a chain of more than {MAX_LINKS} links", + link.display() + ) } /// A repository: the host and the path of its remote URL, in a normal form. @@ -712,6 +719,44 @@ mod tests { assert!(std::fs::read_to_string(&dotfiles).unwrap().contains(".env")); } + /// `count` links in a row in `dir`, and the last one points to + /// `config.toml`. Gives the first link. + fn chain_of_links(dir: &Path, count: usize) -> PathBuf { + for i in 0..count { + let target = if i + 1 == count { + "config.toml".to_string() + } else { + format!("link{}", i + 1) + }; + std::os::unix::fs::symlink(target, dir.join(format!("link{i}"))).unwrap(); + } + dir.join("link0") + } + + #[test] + fn a_chain_of_40_links_is_followed_as_linux_does() { + let tmp = tempfile::TempDir::new().unwrap(); + let first = chain_of_links(tmp.path(), MAX_LINKS); + + one_path().write_to(&first).unwrap(); + + // macOS follows at most 32 links, so read the file at the end of the + // chain and not the chain. + let file = tmp.path().join("config.toml"); + assert!(std::fs::read_to_string(file).unwrap().contains(".env")); + } + + #[test] + fn a_chain_of_41_links_is_refused() { + let tmp = tempfile::TempDir::new().unwrap(); + let first = chain_of_links(tmp.path(), MAX_LINKS + 1); + + let error = one_path().write_to(&first).unwrap_err().to_string(); + + assert!(error.contains("more than 40 links"), "{error}"); + assert!(!tmp.path().join("config.toml").exists()); + } + /// A wt with the same pid, in a different PID namespace or on a /// different host of one home directory, writes the temporary file. #[test] From ab8037f2cd2f11c77b49adf16fce904dfefa77a8 Mon Sep 17 00:00:00 2001 From: Lorenzo Date: Sun, 1 Nov 2026 10:40:57 -0300 Subject: [PATCH 5/6] test: a reader of the old config reads it whole The hook reads the config while a wt share can write it. No test showed that the save renames a new file and does not write into the old one: the tests of a link, of the mode and of a hard link also pass with fs::write. The test opens the config, saves a new one, and reads the old one whole from the open file. With fs::write, it fails. --- src/config.rs | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/src/config.rs b/src/config.rs index 50e5e91..15b474b 100644 --- a/src/config.rs +++ b/src/config.rs @@ -656,6 +656,24 @@ mod tests { toml::from_str("[repos.\"github.com/owner/repo\"]\nlink = [\".env\"]\n").unwrap() } + /// The hook reads the config while a `wt share` can write it. A write + /// into the file empties it first, so a reader can get part of a file. + #[test] + fn a_reader_of_the_old_file_reads_it_whole() { + use std::io::Read; + let tmp = tempfile::TempDir::new().unwrap(); + let path = tmp.path().join("config.toml"); + std::fs::write(&path, "old").unwrap(); + let mut reader = std::fs::File::open(&path).unwrap(); + + one_path().write_to(&path).unwrap(); + + let mut text = String::new(); + reader.read_to_string(&mut text).unwrap(); + assert_eq!(text, "old"); + assert!(std::fs::read_to_string(&path).unwrap().contains(".env")); + } + /// The config can be a link into the dotfiles, also before the dotfiles /// have the file. The file stays a link, its target gets the new text, /// and no temporary file stays. From a24c4d2328abdaf94182e8884247e0a67a0342b8 Mon Sep 17 00:00:00 2001 From: Lorenzo Date: Sun, 1 Nov 2026 10:41:08 -0300 Subject: [PATCH 6/6] docs: the config can be a link, and wt writes it in one step The README did not say how wt writes the config. It now says that the config can be a link into the dotfiles, that a crash or a full disk leaves the old file whole, and that a crash can leave the temporary file, with its name. It names the two cases where wt writes into the old file, a hard link and a mount point, and it says to mount the directory in a container. --- README.md | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index b02ee56..75c1472 100644 --- a/README.md +++ b/README.md @@ -285,7 +285,23 @@ link = [".env.override", ".auth", ".envrc"] The key is the remote URL in a normal form. Each form of the URL gives the same key, so one file is correct for SSH and for HTTPS, and on each machine. -Put this file in your dotfiles to keep the list. +Put this file in your dotfiles to keep the list. The file can be a link into +the dotfiles, as GNU Stow makes it: `wt` changes the file that the link points +to, and the link stays. + +`wt` writes the new file beside the old one, and then renames it over the old +one. A crash or a full disk leaves the old file whole. A crash can also leave +`.config.toml..tmp` beside the file. For a link, it is beside the file +that the link points to, and it has the name of that file. `wt` does not read +it, and you can remove it. While it is there, a later `wt` with the same pid +cannot write the config. + +In two cases `wt` writes into the old file, and a crash can leave part of it. +The file can be a hard link, and a rename would give the new text to one name +only. A container can also mount the config as one file, as +`docker run -v ~/.config/wt/config.toml:...` does, and a rename cannot replace +a mount point. Mount the directory `~/.config/wt` instead to keep the write in +one step. A repository has an entry only when it has a shared path. `wt share` adds the entry with the first path, and `wt init` writes nothing to the file.