在Go语言中同步文件系统(syncfs)

huangapple go评论75阅读模式
英文:

Synchronizing a file system (syncfs) in Go

问题

在Go语言中,有一个名为syscall的包可以用来进行文件系统同步操作。该包提供了FSyncFdatasyncSync等函数来实现文件系统的同步。然而,并没有直接导出syncfs函数。如果你需要使用syncfs函数,你可以考虑使用syscall包中的Sync函数来实现文件系统的同步操作。

英文:

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.

答案1

得分: 5

syncfs只是一个系统调用,在Go语言中很容易触发。

然而,由于syscall没有syncfs系统调用常量,你可以使用golang.org/x/sys/unix代替,该包中已经定义了它(实际上并不是必需的,因为系统调用常量只是一个数字,但使用该包不会有任何问题)。

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
}

为了完整起见,这里是只使用syscall包的解决方案:

import "syscall"

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

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
}

huangapple
  • 本文由 发表于 2015年7月29日 17:58:56
  • 转载请务必保留本文链接:https://go.coder-hub.com/31697163.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定