On Windows, SyncFile currently uses the generic base implementation (src/borg/platform/base.py), which relies on os.fsync(). This does not guarantee data durability on Windows — os.fsync() maps to FlushFileBuffers(), which only flushes the OS cache to the drive's write cache, not necessarily to persistent storage.
The base class already has a TODO (line 154):
A Windows implementation should use CreateFile with FILE_FLAG_WRITE_THROUGH.
Current state
| Platform |
SyncFile |
Data durability mechanism |
| Linux |
Custom (linux.pyx) |
sync_file_range + fdatasync |
| macOS |
Base + custom fdatasync (darwin.pyx) |
F_FULLFSYNC (PR #9385) |
| Windows |
Base fallback |
os.fsync() → FlushFileBuffers() — no write-through guarantee |
Proposed solution
Implement a Windows-native SyncFile in src/borg/platform/windows.pyx that:
- Opens the file using
win32file.CreateFile (or ctypes/msvcrt) with FILE_FLAG_WRITE_THROUGH to bypass the drive write cache
- Overrides
sync() to use FlushFileBuffers() (already what os.fsync() does, but combined with write-through flag it provides stronger guarantees)
- Optionally implements
fdatasync() and sync_dir() for Windows in the platform module
Why this matters
Without FILE_FLAG_WRITE_THROUGH, writes on Windows may sit in the drive's volatile write cache. A power failure or crash can lose data that borg considers durably written, potentially corrupting the repository.
This is the Windows equivalent of the macOS F_FULLFSYNC fix (#9383 / PR #9385).
References
On Windows,
SyncFilecurrently uses the generic base implementation (src/borg/platform/base.py), which relies onos.fsync(). This does not guarantee data durability on Windows —os.fsync()maps toFlushFileBuffers(), which only flushes the OS cache to the drive's write cache, not necessarily to persistent storage.The base class already has a TODO (line 154):
Current state
linux.pyx)sync_file_range+fdatasyncfdatasync(darwin.pyx)F_FULLFSYNC(PR #9385)os.fsync()→FlushFileBuffers()— no write-through guaranteeProposed solution
Implement a Windows-native
SyncFileinsrc/borg/platform/windows.pyxthat:win32file.CreateFile(orctypes/msvcrt) withFILE_FLAG_WRITE_THROUGHto bypass the drive write cachesync()to useFlushFileBuffers()(already whatos.fsync()does, but combined with write-through flag it provides stronger guarantees)fdatasync()andsync_dir()for Windows in the platform moduleWhy this matters
Without
FILE_FLAG_WRITE_THROUGH, writes on Windows may sit in the drive's volatile write cache. A power failure or crash can lose data that borg considers durably written, potentially corrupting the repository.This is the Windows equivalent of the macOS
F_FULLFSYNCfix (#9383 / PR #9385).References
src/borg/platform/base.py:154sync_file_range:src/borg/platform/linux.pyxFILE_FLAG_WRITE_THROUGH: Microsoft docs