4

Is there a package that exports the syncfs function in Go? I'd like to synchronize a particular file system.

I found the syscall package, but it only exports FSync, Fdatasync and Sync.

Petr
  • 62,528
  • 13
  • 153
  • 317

1 Answers1

5

syncfs is just a system call, which are easy to trigger in Go.

However, since the syscall package does not have the syncfs syscall constant, you can use golang.org/x/sys/unix instead, which has it defined (not really necessity, since a syscall constant is just a number, but using that package doesn't hurt).

import "golang.org/x/sys/unix"

func syncfs(fd int) error {
    _, _, err := unix.Syscall(unix.SYS_SYNCFS, uintptr(fd), 0, 0)
    if err != 0 {
        return err
    }
    return nil
}

For completeness, here is the solution just using the syscall package:

import "syscall"

func syncfs(fd int) error {
    _, _, err := syscall.Syscall(306, uintptr(fd), 0, 0)
    if err != 0 {
        return err
    }
    return nil
}