从Go中包装FUSE

时间:2010-11-29 08:25:39

标签: c go fuse

我正在玩用Go包装FUSE。但是我已经陷入了如何处理struct fuse_operations的问题。我似乎无法通过声明type Operations C.struct_fuse_operations来公开操作结构,因为成员是小写的,而我的pure-Go源必须使用C-hackery来设置成员。在这种情况下,我的第一个错误是“无法设置getattr”,看起来是Go等效的默认复制构造函数。我的下一次尝试是公开一个需要GetAttrReadLink等的接口,然后生成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]: cannot use func literal (type func(*_Ctype_char, *_Ctype_struct_stat) int) as type *[0]uint8 in assignment

我不确定要传递给C.struct_fuse_operations的这些成员的内容,我已经看到在一些地方提到它不可能从C调用回Go代码。

如果有可能,我该怎么办?如何为接口函数提供“默认”值,就好像相应的C.struct_fuse_operations成员设置为NULL一样?

1 个答案:

答案 0 :(得分:1)

http://cheesesun.blogspot.com/2010/04/callbacks-in-cgo.html描述了一种通用 - 如果有点复杂 - 技术。 http://groups.google.com/group/golang-nuts/browse_thread/thread/abd0e30dafdbf297?tvc=2&pli=1描述了问题。

我怀疑如果你想把函数指针当作uintptrs之外的其他东西来处理,你就不得不破解cgo。