# windows: Fix atomic write \(\#30234\) · gitcafe/zed

[View on GitCafe](https://git.cafe/gitcafe/zed/commit/20387f24aada21b46a56e437ac9f2d495cfa30dd)

Repository: [gitcafe/zed](https://git.cafe/gitcafe/zed)

Visibility: public

Requested revision: 20387f24aada21b46a56e437ac9f2d495cfa30dd

Requested commit: 20387f24aada21b46a56e437ac9f2d495cfa30dd

Commit: 20387f24aada21b46a56e437ac9f2d495cfa30dd

Tree: ca147c945de61d7e4360428c5dcfb502ef9b2a4a

Author: 张小白

Committer: GitHub

## Message

```
windows: Fix atomic write (#30234)

Superseded #30222

On Windows, `MoveFileExW` fails if another process is holding a handle
to the file. This PR fixes that issue by switching to `ReplaceFileW`
instead.

I’ve also added corresponding tests.

According to [this Microsoft research
paper](https://www.microsoft.com/en-us/research/wp-content/uploads/2006/04/tr-2006-45.pdf)
and the [official
documentation](https://learn.microsoft.com/en-us/windows/win32/fileio/deprecation-of-txf#applications-updating-a-single-file-with-document-like-data),
`ReplaceFileW` is considered an atomic operation. even though the
official docs don’t explicitly state whether `MoveFileExW` or
`ReplaceFileW` is guaranteed to be atomic.

Release Notes:

- N/A
```

## Parents

- [4b5158b1685900ba38ca0603ae5291b2572eba53](https://git.cafe/gitcafe/zed/commit/4b5158b1685900ba38ca0603ae5291b2572eba53?format=markdown)

[Source at this commit](https://git.cafe/gitcafe/zed/tree/20387f24aada21b46a56e437ac9f2d495cfa30dd?format=markdown)
