【问题标题】:Wrapping FUSE from Go从 Go 封装 FUSE
【发布时间】:2011-05-17 03:53:44
【问题描述】:

我正在尝试用 Go 封装 FUSE。但是我一直坚持如何处理struct fuse_operations。我似乎无法通过声明 type Operations C.struct_fuse_operations 来公开操作结构,因为成员是小写的,而且我的纯 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]: 不能在赋值中使用 func 字面量 (type func(*_Ctype_char, *_Ctype_struct_stat) int) 作为类型 *[0]uint8

我不确定要传递给C.struct_fuse_operations 的这些成员什么,而且我已经看到在一些地方提到无法从 C 调用回 Go 代码。

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

【问题讨论】:

  • 我想将赏金奖励给解决我的界面问题的答案,而不是专门针对 FUSE。

标签: c go fuse


【解决方案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。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-02-23
    • 2014-12-16
    • 1970-01-01
    • 2013-03-06
    • 2018-08-24
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多