Skip to content

platform: implement Windows-native SyncFile using FILE_FLAG_WRITE_THROUGH #9388

@mr-raj12

Description

@mr-raj12

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:

  1. Opens the file using win32file.CreateFile (or ctypes/msvcrt) with FILE_FLAG_WRITE_THROUGH to bypass the drive write cache
  2. Overrides sync() to use FlushFileBuffers() (already what os.fsync() does, but combined with write-through flag it provides stronger guarantees)
  3. 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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions