从 Go 封装 FUSE

发布于 2024-10-05 07:04:23 字数 1655 浏览 3 评论 0原文

我正在尝试用 Go 包装 FUSE。然而,我一直不知道如何处理 struct fusion_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]: 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 一样?

I'm playing around with wrapping FUSE with Go. However I've come stuck with how to deal with struct fuse_operations. I can't seem to expose the operations struct by declaring type Operations C.struct_fuse_operations as the members are lower case, and my pure-Go sources would have to use C-hackery to set the members anyway. My first error in this case is "can't set getattr" in what looks to be the Go equivalent of a default copy constructor. My next attempt is to expose an interface that expects GetAttr, ReadLink etc, and then generate C.struct_fuse_operations and bind the function pointers to closures that call the given interface.

This is what I've got (explanation continues after code):

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))
}

This gives the following error:

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

I'm not sure what to pass to these members of C.struct_fuse_operations, and I've seen mention in a few places it's not possible to call from C back into Go code.

If it is possible, what should I do? How can I provide the "default" values for interface functions that acts as though the corresponding C.struct_fuse_operations member is set to NULL?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

爱格式化 2024-10-12 07:04:23

http://cheesesun.blogspot.com/2010/04/callbacks-in -cgo.html 描述了一种通用的(虽然有些复杂)技术。 http://groups.google.com /group/golang-nuts/browse_thread/thread/abd0e30dafdbf297?tvc=2&a​​mp;pli=1 描述了该问题。

我怀疑如果你想将函数指针作为 uintptrs 以外的任何东西来处理,你将不得不破解 cgo。

http://cheesesun.blogspot.com/2010/04/callbacks-in-cgo.html describes a general - if somewhat convoluted - technique. http://groups.google.com/group/golang-nuts/browse_thread/thread/abd0e30dafdbf297?tvc=2&pli=1 describes the problem.

I suspect that if you want to handle the function pointers as anything other than uintptrs, you'll have to hack on cgo.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文