2016-11-21 17:09:29 +00:00
|
|
|
// Copyright (C) 2016 The Syncthing Authors.
|
|
|
|
//
|
|
|
|
// This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
// License, v. 2.0. If a copy of the MPL was not distributed with this file,
|
2017-02-09 06:52:18 +00:00
|
|
|
// You can obtain one at https://mozilla.org/MPL/2.0/.
|
2016-11-21 17:09:29 +00:00
|
|
|
|
|
|
|
package osutil
|
|
|
|
|
|
|
|
import (
|
|
|
|
"os"
|
|
|
|
"runtime"
|
|
|
|
)
|
|
|
|
|
|
|
|
func SyncFile(path string) error {
|
|
|
|
flag := 0
|
|
|
|
if runtime.GOOS == "windows" {
|
|
|
|
flag = os.O_WRONLY
|
|
|
|
}
|
|
|
|
fd, err := os.OpenFile(path, flag, 0)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer fd.Close()
|
|
|
|
// MacOS and Windows do not flush the disk cache
|
2016-12-18 18:57:41 +00:00
|
|
|
return fd.Sync()
|
2016-11-21 17:09:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func SyncDir(path string) error {
|
|
|
|
if runtime.GOOS == "windows" {
|
|
|
|
// not supported by Windows
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return SyncFile(path)
|
|
|
|
}
|