我正在使用Fuse API来实现一个虚拟文件系统,该系统将目录结构保存在树状数据结构中。
我可以这样实现我的用户空间getattr()和readdir():
static int my_getattr(const char *path, struct stat *stbuf,
struct fuse_file_info *fi)
{
(void) fi;
int res = 0;
memset(stbuf, 0, sizeof(struct stat));
Node* myNode = theTree_.findNode(path, '/');
if (myNode != nullptr)
{
if (myNode->theValue == RfsFuseVfs::NON_LEAF)
{
puts("directory!");
stbuf->st_mode = S_IFDIR | 0755;
stbuf->st_nlink = 2;
}
else
{
puts("file!");
stbuf->st_mode = S_IFREG | 0444;
stbuf->st_nlink = 1;
stbuf->st_size = 100;
}
}
else
{
puts("not found");
res = -ENOENT;
}
return res;
}
static int rfs_fuse_vfs_readdir(const char *path, void *buf,
fuse_fill_dir_t filler,
off_t offset, struct fuse_file_info *fi,
enum fuse_readdir_flags flags)
{
(void) offset;
(void) fi;
(void) flags;
Node* myCurrDir = nullptr;
if (strcmp(path, "/") == 0)
{
myCurrDir = &theTree.getRootNode();
}
else
{
myCurrDir = theTree.findNode(path, '/');
}
if (myCurrDir != nullptr)
{
filler(buf, ".", NULL, 0, static_cast<fuse_fill_dir_flags>(0));
filler(buf, "..", NULL, 0, static_cast<fuse_fill_dir_flags>(0));
for (const auto& childNode : *myCurrDir)
{
filler(buf, childNode.theName.c_str(), NULL, 0, static_cast<fuse_fill_dir_flags>(0));
}
return 0;
}
else
{
return -ENOENT;
}
}
这可以让我通过目录层次结构,但是当我尝试执行cd更改目录时,它实际上并没有更改目录。
我想念什么?