sync.go 741 B

12345678910111213141516171819202122232425262728293031323334353637
  1. // Copyright (C) 2016 The Syncthing Authors.
  2. //
  3. // This Source Code Form is subject to the terms of the Mozilla Public
  4. // License, v. 2.0. If a copy of the MPL was not distributed with this file,
  5. // You can obtain one at http://mozilla.org/MPL/2.0/.
  6. package osutil
  7. import (
  8. "os"
  9. "runtime"
  10. )
  11. func SyncFile(path string) error {
  12. flag := 0
  13. if runtime.GOOS == "windows" {
  14. flag = os.O_WRONLY
  15. }
  16. fd, err := os.OpenFile(path, flag, 0)
  17. if err != nil {
  18. return err
  19. }
  20. defer fd.Close()
  21. // MacOS and Windows do not flush the disk cache
  22. if err := fd.Sync(); err != nil {
  23. return err
  24. }
  25. return nil
  26. }
  27. func SyncDir(path string) error {
  28. if runtime.GOOS == "windows" {
  29. // not supported by Windows
  30. return nil
  31. }
  32. return SyncFile(path)
  33. }