Repository navigation
Replace an existing config file on Windows instead of failing - #122
Merged
Merged
Conversation
…ling `std::fs::rename` refuses when the destination exists on Windows. Every call to `write_atomic` has an existing destination -- it rewrites the client configuration the user already has -- so adoption did not merely degrade on that platform, it failed at the first step, reporting "the file exists" about a file whose existence is the entire point. `MoveFileExW` with `MOVEFILE_REPLACE_EXISTING` is the same-volume atomic replace that `rename(2)` already is on unix. Not "delete the destination, then move". That leaves a window in which the user's configuration file does not exist, and a process that dies inside it takes the original with it -- before we have told them where the backup went. `MOVEFILE_WRITE_THROUGH` so the move is on disk before the call returns. This writes other people's configuration files, and "we said it was saved and it was not there after a power cut" is far harder to track down than a write that failed outright. The new test reads as a no-op on unix, where rename has always overwritten. It is a stake driven in for the other platform. Found by the Windows CI job in #119, which is stacked on this and is what confirms the fix. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
Closed
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.
A real bug, found by the Windows CI job in #119 — which is stacked on this and is what confirms the fix.
std::fs::renamerefuses when the destination exists on Windows. Every call towrite_atomichas an existing destination — it rewrites the client configuration the user already has. So adoption did not merely degrade on that platform: it failed at the first step, reporting "the file exists" about a file whose existence is the entire point of the operation.Four tests caught it (
the_backup_holds_what_was_there_before,back_to_back_real_backups_all_survive,two_backups_in_the_same_millisecond_do_not_overwrite_each_other,removing_takes_only_that_one_server) — all withOs { code: 80, kind: AlreadyExists }.The fix
MoveFileExWwithMOVEFILE_REPLACE_EXISTING— the same-volume atomic replace thatrename(2)already is on unix.Not "delete the destination, then move." That leaves a window in which the user's configuration file does not exist, and a process that dies inside it takes the original with it — before we have told them where the backup went. This module's whole reason for existing is not damaging other people's files.
MOVEFILE_WRITE_THROUGHso the move is on disk before the call returns. This writes other people's configuration, and "we said it was saved and it was not there after a power cut" is far harder to track down than a write that failed outright.The new test
It reads as a no-op on unix, where rename has always overwritten. It is a stake driven in for the other platform — and it also checks no temporary file is left behind.
What this does not preserve
On unix
write_atomickeeps the original file's mode (there is a comment about not taking it upon ourselves to chmod someone else's file). On Windows the replaced file takes the directory's inherited ACL rather than the destination's own.ReplaceFileWwould preserve it, at the cost of a second code path for the create-new case. Worth revisiting if it turns out to matter; flagging it rather than leaving it silent.🤖 Generated with Claude Code