sync.go 696 B

12345678910111213141516171819202122232425262728293031323334
  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. return fd.Sync()
  23. }
  24. func SyncDir(path string) error {
  25. if runtime.GOOS == "windows" {
  26. // not supported by Windows
  27. return nil
  28. }
  29. return SyncFile(path)
  30. }