1
0
mirror of https://github.com/restic/restic.git synced 2024-07-02 08:40:55 +02:00
restic/internal/fs/setflags_linux_test.go
greatroar 7080fed7ae Set O_NOATIME flag on Linux
Citing Kerrisk, The Linux Programming Interface:

    The O_NOATIME flag is intended for use by indexing and backup
    programs. Its use can significantly reduce the amount of disk
    activity, because repeated disk seeks back and forth across the
    disk are not required to read the contents of a file and to update
    the last access time in the file’s i-node[.]

restic used to do this, but the functionality was removed along with the
fadvise call in #670.
2022-02-06 15:00:34 +01:00

68 lines
1.3 KiB
Go

package fs
import (
"io"
"io/ioutil"
"os"
"testing"
"time"
rtest "github.com/restic/restic/internal/test"
"golang.org/x/sys/unix"
)
func TestNoatime(t *testing.T) {
f, err := ioutil.TempFile("", "restic-test-noatime")
if err != nil {
t.Fatal(err)
}
defer func() {
_ = f.Close()
err = Remove(f.Name())
if err != nil {
t.Fatal(err)
}
}()
// Only run this test on common filesystems that support O_NOATIME.
// On others, we may not get an error.
if !supportsNoatime(t, f) {
t.Skip("temp directory may not support O_NOATIME, skipping")
}
// From this point on, we own the file, so we should not get EPERM.
_, err = io.WriteString(f, "Hello!")
rtest.OK(t, err)
_, err = f.Seek(0, io.SeekStart)
rtest.OK(t, err)
getAtime := func() time.Time {
info, err := f.Stat()
rtest.OK(t, err)
return ExtendedStat(info).AccessTime
}
atime := getAtime()
err = setFlags(f)
rtest.OK(t, err)
_, err = f.Read(make([]byte, 1))
rtest.OK(t, err)
rtest.Equals(t, atime, getAtime())
}
func supportsNoatime(t *testing.T, f *os.File) bool {
var fsinfo unix.Statfs_t
err := unix.Fstatfs(int(f.Fd()), &fsinfo)
rtest.OK(t, err)
return fsinfo.Type == unix.BTRFS_SUPER_MAGIC ||
fsinfo.Type == unix.EXT2_SUPER_MAGIC ||
fsinfo.Type == unix.EXT3_SUPER_MAGIC ||
fsinfo.Type == unix.EXT4_SUPER_MAGIC ||
fsinfo.Type == unix.TMPFS_MAGIC
}