Skip to content

Commit d6bf5e5

Browse files
committed
fs backend: synchronize tmp file with device as much as possible
1 parent 3704775 commit d6bf5e5

File tree

1 file changed

+8
-1
lines changed

1 file changed

+8
-1
lines changed

backends/fs/fs.go

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
"path/filepath"
88
"sort"
99
"strings"
10+
"syscall"
1011

1112
"github.com/PowerDNS/simpleblob"
1213
)
@@ -72,9 +73,15 @@ func (b *Backend) Store(ctx context.Context, name string, data []byte) error {
7273
}
7374
fullPath := filepath.Join(b.rootPath, name)
7475
tmpPath := fullPath + ".tmp" // ignored by List()
75-
if err := os.WriteFile(tmpPath, data, 0o644); err != nil {
76+
flags := os.O_WRONLY | os.O_CREATE | os.O_TRUNC | os.O_SYNC | syscall.O_DIRECT
77+
f, err := os.OpenFile(tmpPath, flags, 0644)
78+
if err != nil {
7679
return err
7780
}
81+
_, err = f.Write(data)
82+
if err1 := f.Close(); err1 != nil && err == nil {
83+
err = err1
84+
}
7885
return os.Rename(tmpPath, fullPath)
7986
}
8087

0 commit comments

Comments
 (0)