Skip to content

fix: wt writes the config in one step - #23

Merged
lorenzolfm merged 6 commits into
mainfrom
issue-22
Nov 1, 2026
Merged

lorenzolfm merged 6 commits into
mainfrom
issue-22

Conversation

@lorenzolfm

@lorenzolfm lorenzolfm commented Nov 1, 2026 •

Copy link
Copy Markdown
Owner

Config::save wrote the config with fs::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 a wt share can write it, so it could read part of a file.

  • write_in_one_step writes 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 two wt share at 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 with File::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.
  • The new file keeps the mode, the owner and the group of the old one, as fs::write did. After sudo 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_links reads 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 use fs::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::write made the file there.
  • The README says it, and it says that a crash can leave the temporary file.

No lock, as the issue says. Two wt share at the same time can still lose one update, and wt share again 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 test runs 79 tests.

  • One test opens the config, writes a new one, and reads the old one whole from the open file. With fs::write, it fails.
  • One test writes through a relative link to a dotfiles file, as Stow makes it. The link stays, and the file has the new body. With a rename over the link, it fails.
  • One test does the same with a link to a file that is not present. With fs::canonicalize, it fails.
  • One test writes through a chain of 40 links. With the first loop, which did not look at the target of the 40th link, it fails. One test refuses 41.
  • One test puts a file at the temporary name first. The write fails, and that file and the config stay as they are. With File::create, it fails.

Manual test with XDG_CONFIG_HOME and a relative link to a dotfiles config with mode 600:

Case Result
wt share .env The link stays. The dotfiles file has the new entry and the old one, with mode 600. No temporary file.
wt share .env with the dotfiles file in group wheel and mode 640 The group and the mode stay.
wt share data with the dotfiles directory read-only wt 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.

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.
@lorenzolfm
lorenzolfm merged commit d61921a into main Nov 1, 2026
4 checks passed
@lorenzolfm
lorenzolfm deleted the issue-22 branch November 9, 2026 16:54
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

wt share can leave a config file that wt cannot read

1 participant