Skip to content

Replace an existing config file on Windows instead of failing - #122

Merged
fylorn merged 1 commit into
mainfrom
fix/windows-atomic-replace
Sep 23, 2026
Merged

fylorn merged 1 commit into
mainfrom
fix/windows-atomic-replace

Conversation

@fylorn

@fylorn fylorn commented Sep 23, 2026

Copy link
Copy Markdown
Contributor

A real bug, found by the Windows CI job in #119 — which is stacked on this and is what confirms the fix.

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 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 with Os { code: 80, kind: AlreadyExists }.

The fix

MoveFileExW with MOVEFILE_REPLACE_EXISTING — 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. This module's whole reason for existing is not damaging other people's files.

MOVEFILE_WRITE_THROUGH so 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_atomic keeps 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. ReplaceFileW would 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

…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>
@fylorn
fylorn merged commit af44378 into main Sep 23, 2026
1 check passed
@fylorn
fylorn deleted the fix/windows-atomic-replace branch September 23, 2026 06:17
@fylorn fylorn mentioned this pull request Sep 23, 2026
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.

1 participant