Make atomic file replacement crash durable

This commit is contained in:
2026-08-10 17:44:38 +00:00
parent 1dccf5f140
commit 59f3fe3d1d
13 changed files with 502 additions and 233 deletions

View File

@@ -4,6 +4,7 @@ package fileops
import (
"errors"
"fmt"
"golang.org/x/sys/windows"
)
@@ -29,12 +30,12 @@ func syncDirectory(path string) error {
err = windows.FlushFileBuffers(directory)
// Windows filesystems may reject flushing a directory handle even when it
// was opened correctly. Preserve every error except the documented forms
// that mean this operation is unavailable for the handle or filesystem.
// was opened correctly. Report documented unavailable forms explicitly so
// callers do not confuse visible replacement with a durable one.
if errors.Is(err, windows.ERROR_INVALID_FUNCTION) ||
errors.Is(err, windows.ERROR_INVALID_HANDLE) ||
errors.Is(err, windows.ERROR_NOT_SUPPORTED) {
return nil
return fmt.Errorf("%w for %q: %w", ErrDirectorySyncUnsupported, path, err)
}
return err
}