lib/model: Add fsync of files and directories, option to disable (fixes #3711)

This commit is contained in:
Unrud
2016-11-21 18:09:29 +01:00
committed by Jakob Borg
parent 51e10e344d
commit 1574b7d834
8 changed files with 126 additions and 1 deletions

View File

@@ -77,6 +77,11 @@ func (w *AtomicWriter) Close() error {
// Try to not leave temp file around, but ignore error.
defer os.Remove(w.next.Name())
if err := w.next.Sync(); err != nil {
w.err = err
return err
}
if err := w.next.Close(); err != nil {
w.err = err
return err
@@ -97,6 +102,8 @@ func (w *AtomicWriter) Close() error {
return err
}
SyncDir(filepath.Dir(w.next.Name()))
// Set w.err to return appropriately for any future operations.
w.err = ErrClosed

37
lib/osutil/sync.go Normal file
View File

@@ -0,0 +1,37 @@
// 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,
// You can obtain one at http://mozilla.org/MPL/2.0/.
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
if err := fd.Sync(); err != nil {
return err
}
return nil
}
func SyncDir(path string) error {
if runtime.GOOS == "windows" {
// not supported by Windows
return nil
}
return SyncFile(path)
}