You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

43 lines
818 B

  1. //go:build darwin
  2. // +build darwin
  3. package backend
  4. import (
  5. "syscall"
  6. "golang.org/x/sys/unix"
  7. )
  8. const (
  9. // Using default File.Sync function, same as fcntl(fd, F_FULLFSYNC)
  10. DM_SYNC = 1
  11. // Using syscall.Fsync function, for MacOS this is not safe but is very fast.
  12. DM_FSYNC = 2
  13. // Using fcntl with F_BARRIERFSYNC parameter, for more details please refer:
  14. // https://developer.apple.com/documentation/xcode/reducing-disk-writes
  15. DM_BFSYNC = 3
  16. F_BARRIERFSYNC = 85
  17. )
  18. var (
  19. // By default using F_BARRIERFSYNC
  20. DarwinSyncMode = DM_BFSYNC
  21. )
  22. func (df *DiskFile) Sync() error {
  23. switch DarwinSyncMode {
  24. case DM_SYNC:
  25. return df.File.Sync()
  26. case DM_BFSYNC:
  27. fd := df.File.Fd()
  28. _, err := unix.FcntlInt(fd, F_BARRIERFSYNC, 0)
  29. return err
  30. default:
  31. fd := df.File.Fd()
  32. return syscall.Fsync(int(fd))
  33. }
  34. }