diff --git a/logtail/filch/filch.go b/logtail/filch/filch.go index 07d9b6203..22740588e 100644 --- a/logtail/filch/filch.go +++ b/logtail/filch/filch.go @@ -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 } diff --git a/logtail/filch/openfilesync_darwin.go b/logtail/filch/openfilesync_darwin.go new file mode 100644 index 000000000..f6bb9183a --- /dev/null +++ b/logtail/filch/openfilesync_darwin.go @@ -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 +} diff --git a/logtail/filch/openfilesync_notdarwin.go b/logtail/filch/openfilesync_notdarwin.go new file mode 100644 index 000000000..f6cdb264c --- /dev/null +++ b/logtail/filch/openfilesync_notdarwin.go @@ -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) +}