42 lines
1.0 KiB
Go
42 lines
1.0 KiB
Go
//go:build windows
|
|
|
|
package fileops
|
|
|
|
import (
|
|
"errors"
|
|
"fmt"
|
|
|
|
"golang.org/x/sys/windows"
|
|
)
|
|
|
|
func syncDirectory(path string) error {
|
|
pathPointer, err := windows.UTF16PtrFromString(path)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
directory, err := windows.CreateFile(
|
|
pathPointer,
|
|
windows.GENERIC_WRITE,
|
|
windows.FILE_SHARE_READ|windows.FILE_SHARE_WRITE|windows.FILE_SHARE_DELETE,
|
|
nil,
|
|
windows.OPEN_EXISTING,
|
|
windows.FILE_FLAG_BACKUP_SEMANTICS,
|
|
0,
|
|
)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
defer func() { _ = windows.CloseHandle(directory) }()
|
|
|
|
err = windows.FlushFileBuffers(directory)
|
|
// Windows filesystems may reject flushing a directory handle even when it
|
|
// 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 fmt.Errorf("%w for %q: %w", ErrDirectorySyncUnsupported, path, err)
|
|
}
|
|
return err
|
|
}
|