英文:
Synchronizing a file system (syncfs) in Go
问题
在Go语言中,有一个名为syscall的包可以用来进行文件系统同步操作。该包提供了FSync、Fdatasync和Sync等函数来实现文件系统的同步。然而,并没有直接导出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
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。


评论