我正在尝试用Go语言包装FUSE。然而,我遇到了处理struct fuse_operations的问题。我似乎无法通过声明"type Operations C.struct_fuse_operations"来暴露操作结构,因为其成员是小写的,而且我的纯Go源代码必须使用C-hackery设置成员。在这种情况下,我的第一个错误是“无法设置getattr”,就像默认复制构造函数的Go等效项一样。我的下一步尝试是公开一个接口,该接口期望GetAttr、ReadLink等,并生成C.struct_fuse_operations,将函数指针绑定到调用给定接口的闭包中。下面是代码示例:
这会产生以下错误:
如果可能的话,我该怎么做?如何为充当对应
package fuse
// #include <fuse.h>
// #include <stdlib.h>
import "C"
import (
//"fmt"
"os"
"unsafe"
)
type Operations interface {
GetAttr(string, *os.FileInfo) int
}
func Main(args []string, ops Operations) int {
argv := make([]*C.char, len(args) + 1)
for i, s := range args {
p := C.CString(s)
defer C.free(unsafe.Pointer(p))
argv[i] = p
}
cop := new(C.struct_fuse_operations)
cop.getattr = func(*C.char, *C.struct_stat) int {}
argc := C.int(len(args))
return int(C.fuse_main_real(argc, &argv[0], cop, C.size_t(unsafe.Sizeof(cop)), nil))
}
package main
import (
"fmt"
"fuse"
"os"
)
type CpfsOps struct {
a int
}
func (me *CpfsOps) GetAttr(string, *os.FileInfo) int {
return -1;
}
func main() {
fmt.Println(os.Args)
ops := &CpfsOps{}
fmt.Println("fuse main returned", fuse.Main(os.Args, ops))
}
这会产生以下错误:
fuse.go:21[fuse.cgo1.go:23]: 无法将函数文字(类型为 func(*_Ctype_char, *_Ctype_struct_stat) int)用作 *[0]uint8 类型的赋值我不确定要传递什么给
C.struct_fuse_operations
的这些成员,我在一些地方看到提到从C回调到Go代码是不可能的。如果可能的话,我该怎么做?如何为充当对应
C.struct_fuse_operations
成员设置为NULL的接口函数提供“默认”值?