Petr
Petr

Reputation: 63359

Synchronizing a file system (syncfs) in Go

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.

Upvotes: 4

Views: 2042

Answers (1)

user142162
user142162

Reputation:

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
}

Upvotes: 5

Related Questions