File system sync (syncfs) in Go

Is there a package that exports a function syncfs

to Go? I would like to sync a specific filesystem.

I found the syscall package, but it only exports FSync

, Fdatasync

and Sync

.

+3


source to share


1 answer


syncfs

is just a system call that runs easily in Go.

However, since syscall

package
does not have a syscall constant syncfs

, you can use the golang.org/x/sys/unix

one it defines (actually not necessary, since the syscall constant is just a number, but it doesn't hurt to use that package).

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 a solution just using the package syscall

:

import "syscall"

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

      

+3


source







All Articles