Repository navigation
fix: wt writes the config in one step - #23
Merged
Merged
Conversation
This was referenced Nov 1, 2026
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.
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.
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.
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.
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.
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Config::savewrote the config withfs::write, which empties the file and then writes it. A crash or a full disk between the two left part of a file, and each command that reads the config, and the hook, then refused it. The hook also reads the config while awt sharecan write it, so it could read part of a file.write_in_one_stepwrites a temporary file beside the config,.config.toml.<pid>.tmp, syncs it to the disk and renames it over the config. The pid is in the name, so twowt shareat the same time do not write into one temporary file. Two processes in two PID namespaces, or on two hosts of one home, can have the same pid, so wt makes the file withFile::create_new: a file that is present stops the write. When a step fails, wt removes the temporary file that it made, and the config does not change.fs::writedid. Aftersudo wt share, root gives the file back to its owner. A user can give the file only to a group of that user, and else the file is the user's.follow_linksreads the chain of links first, at most 40 as Linux does, and the rename replaces the file at the end of it. A rename over a Stow link makes it a regular file. I did not usefs::canonicalize, as the issue proposes: it fails on a link to a file that is not present, and the rename then replaced the link.fs::writemade the file there.No lock, as the issue says. Two
wt shareat the same time can still lose one update, andwt shareagain fixes it. A hard link to the config breaks at the rename. git breaks a hard link at each checkout too, so I left it.Follow-ups: #25, #26.
Closes #22
Test
The four CI gates (
nix build .#wt .#wt-clippy .#wt-test .#wt-fmt) pass.cargo testruns 79 tests.fs::write, it fails.fs::canonicalize, it fails.File::create, it fails.Manual test with
XDG_CONFIG_HOMEand a relative link to a dotfiles config with mode 600:wt share .envwt share .envwith the dotfiles file in groupwheeland mode 640wt share datawith the dotfiles directory read-onlywt cannot write .../dotfiles/wt/.config.toml.<pid>.tmp: Permission denied. The config does not change, and no temporary file stays.I did not test the owner under
sudo.