filch: use F_NOCACHE on macOS

For #1320

Signed-off-by: David Crawshaw <crawshaw@tailscale.com>
This commit is contained in:
David Crawshaw 2021-02-17 12:18:30 -08:00
parent dec01ef22b
commit de94fe0f87
3 changed files with 45 additions and 2 deletions

View File

@ -131,11 +131,11 @@ func New(filePrefix string, opts Options) (f *Filch, err error) {
path1 := filePrefix + ".log1.txt"
path2 := filePrefix + ".log2.txt"
f1, err = os.OpenFile(path1, os.O_CREATE|os.O_RDWR, 0600)
f1, err = openFileSync(path1, os.O_CREATE|os.O_RDWR, 0600)
if err != nil {
return nil, err
}
f2, err = os.OpenFile(path2, os.O_CREATE|os.O_RDWR, 0600)
f2, err = openFileSync(path2, os.O_CREATE|os.O_RDWR, 0600)
if err != nil {
return nil, err
}

View File

@ -0,0 +1,27 @@
// Copyright (c) 2021 Tailscale Inc & AUTHORS All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
//+build darwin
package filch
import (
"fmt"
"os"
"syscall"
"golang.org/x/sys/unix"
)
func openFileSync(path string, flag int, perm os.FileMode) (*os.File, error) {
f, err := os.OpenFile(path, flag, perm)
if err != nil {
return nil, err
}
_, err = unix.FcntlInt(uintptr(f.Fd()), syscall.F_NOCACHE, 1)
if err != nil {
return nil, fmt.Errorf("openFileSync: %w", err)
}
return f, nil
}

View File

@ -0,0 +1,16 @@
// Copyright (c) 2021 Tailscale Inc & AUTHORS All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
//+build !darwin
package filch
import (
"os"
)
func openFileSync(path string, flag int, perm os.FileMode) (*os.File, error) {
// TODO(crawshaw): on Linux and FreeBSD, use O_SYNC
return os.OpenFile(path, flag, perm)
}