c++ - 为什么我的回调没有被调用?

标签 c++ callback g++ fuse

我正在尝试用 C++(使用 g++ 编译器)实现 fuse hello 示例 ( https://lastlog.de/misc/fuse-doc/doc/html/hello_8c.html )。为此,我必须对其进行一些更改。

#define FUSE_USE_VERSION 30

#include <cstdlib>
#include <iostream>

#include <fuse.h>
#include <stdio.h>
#include <string.h>
#include <errno.h>
#include <fcntl.h>

static const char *hello_str = "Hello World!\n";

static int hello_getattr(const char *path, struct stat *stbuf)
{
    int res = 0;

    memset(stbuf, 0, sizeof(struct stat));

    if (strcmp(path, "/") == 0)
    {
        stbuf->st_mode = S_IFDIR | 0777;
        stbuf->st_nlink = 2;
    }
    else if (strcmp(path, "/hello") == 0)
    {
        stbuf->st_mode = S_IFREG | 0444;
        stbuf->st_nlink = 1;
        stbuf->st_size = strlen(hello_str);
    }
    else
        res = -ENOENT;

    return res;
};

static int hello_readdir(const char *path, void *buf, fuse_fill_dir_t filler, off_t offset, struct fuse_file_info *fi)
{
    (void) offset;
    (void) fi;

    if (strcmp(path, "/") != 0)
        return -ENOENT;

    filler(buf, ".", NULL, 0);
    filler(buf, "..", NULL, 0);
    filler(buf, "hello", NULL, 0);

    return 0;
};

static int hello_open(const char *path, struct fuse_file_info *fi)
{
    if (strcmp(path, "/hello") != 0)
        return -ENOENT;
    if ((fi->flags & 3) != O_RDONLY)
        return -EACCES;
    return 0;
};

static int hello_read(const char *path, char *buf, size_t size, off_t offset, struct fuse_file_info *fi)
{
    size_t len;
    (void) fi;
    if(strcmp(path, "/hello") != 0)
        return -ENOENT;
    len = strlen(hello_str);
    if (offset < len) {
        if (offset + size > len)
            size = len - offset;
        memcpy(buf, hello_str + offset, size);
    } else
        size = 0;
    return size;
};

static int hello_opendir(const char *path, struct fuse_file_info *fi)
{
    return 0;
};

struct hello_fuse_operations : fuse_operations
{
    hello_fuse_operations()
    {
        this->getattr = hello_getattr;
        this->open = hello_open;
        this->read = hello_read;
        this->opendir = hello_opendir;
        this->readdir = hello_readdir;
    }
};

static struct hello_fuse_operations hello_oper;

int main(int argc, char** argv)
{
    return fuse_main_real(argc, argv, &hello_oper, sizeof(&hello_oper), NULL);
}

我的问题是,fuse 说 readreaddir 没有实现,也没有调用它们,即使它们显然已经实现了。

unique: 13, opcode: READDIR (28), nodeid: 1, insize: 80, pid: 16153
   unique: 13, error: -38 (Function not implemented), outsize: 16

我似乎做错了一些基本错误,但我想不通是什么。 (我对 C++ 也不是很有经验)

如何让示例运行?

最佳答案

sizeof(&hello_oper) 看起来像个错误 - 这是指针的大小,而不是结构的大小。

sizeof(fuse_operations) 可能是您想要的。 sizeof hello_oper 在这种情况下是相同的,但是如果您稍后将数据成员添加到 hello_fuse_operations 那么它就会出错。

关于c++ - 为什么我的回调没有被调用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45516824/

相关文章:

c++ - 从 DLL 导出 ASM 函数 - Visual Studio C++

c++ - curl_easy_setopt()中设置的回调函数会被调用多少次?

javascript - 无法修改javascript回调中的值

jQuery ajax 方法使用回调来更新元素类

c++ - 通过静态库进行条件链接

c++ - Win32 UI - 使控件随着父控件的扩展而扩展

c++ - 从Lambda中的空指针调用方法

c++ - `__m256` 的包装器使用构造函数产生段错误 - Windows 64 + MinGW + AVX 问题

c++ - 在哪里可以查看 GCC 对实现定义行为的实现?

c++ - VC++编译器忽略重载函数