2017-08-04 205 views
0

我試圖在C++(使用g ++編譯器)中實現保險絲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); 
} 

我的問題是,保險絲說readreaddir不落實,不給他們打電話,即使他們清楚地得到落實。

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

它幾乎看起來像我在做一些基本的錯誤,但我無法弄清楚什麼。 (我對C++也不是很有經驗)

我如何得到這個例子的工作?

+1

IDK有關保險絲的任何信息,但'sizeof(&hello_oper)'看起來像是一個錯誤。我猜'sizeof(fuse_operations)'是你想要的。 –

+0

@ M.M哇,謝謝。你想做出答案並收集一些觀點嗎? –

回答

1

sizeof(&hello_oper)看起來像一個錯誤 - 這是一個指針的大小,而不是一個結構的大小。

sizeof(fuse_operations)可能是你想要的。 sizeof hello_oper在這種情況下將是相同的,但如果您稍後將數據成員添加到hello_fuse_operations那麼它會變成錯誤的。

相關問題