从Go中包装FUSE

Mat*_*ner 5 c fuse go

我正在玩用Go包裹FUSE.但是我已经陷入了如何应对的困境struct fuse_operations.我似乎无法通过声明type Operations C.struct_fuse_operations成员是小写来暴露操作结构,而我的pure-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))
}
Run Code Online (Sandbox Code Playgroud)

这会出现以下错误:

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一样?

Iai*_*ain 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。