用FUSE写简单的文件系统

lym1108csu 2014-04-22 09:30:57
问下CSDN里面的大牛们点问题。。
我们作业要写个简易的file system用到FUSE。我大概看明白了hello.c这个文件。。。可以实现在mount 的文件夹下建立一个hello文件,,但是我不会在mount的文件夹下先建立一个文件夹,然后再在文件夹里面建立hello 文件
最后的效果应该可以这样访问
cat mountfile/folderIcreate/hello
以下是代码,跟hello.c差不多:

#define FUSE_USE_VERSION 30

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

static const char *hello_str = "Hello World!\n";
static const char *add_path = "/add";
static const char *div_path = "/div";
static const char *hello_path = "/hello";

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

printf("%s", path);

memset(stbuf, 0, sizeof(struct stat));
if (strcmp(path, "/") == 0)
{
stbuf->st_mode = S_IFDIR | 0755;
stbuf->st_nlink = 2;
}
else if (strcmp(path, hello_path) == 0)
{
stbuf->st_mode = S_IFREG | 0444;
stbuf->st_nlink = 1;
stbuf->st_size = strlen(hello_str);
}
else if (strcmp(path, add_path) == 0)
{
stbuf->st_mode = S_IFDIR | 0755;
stbuf->st_nlink = 3;
if (strcmp(path, add_path1) == 0)
{
stbuf->st_mode = S_IFREG | 0444;
stbuf->st_nlink = 1;
stbuf->st_size = strlen(hello_str);
}
}
else if(strcmp(path, div_path) == 0)
{
stbuf->st_mode = S_IFDIR | 0755;
stbuf->st_nlink = 3;
}
else if(strcmp(path, div_path) == 0)
{
stbuf->st_mode = S_IFDIR | 0755;
stbuf->st_nlink = 3;
}
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)
{
printf("%s", path);
(void) offset;
(void) fi;

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

filler(buf, ".", NULL, 0);
filler(buf, "..", NULL, 0);
filler(buf, hello_path + 1, NULL, 0);
filler(buf, add_path + 1, NULL, 0);
filler(buf, div_path + 1, NULL, 0);

return 0;
}

static int hello_open(const char *path, struct fuse_file_info *fi)
{
printf("%s", path);
if (strcmp(path, hello_path) != 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)
{
printf("%s", path);
size_t len;
(void) fi;
if(strcmp(path, hello_path) != 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 struct fuse_operations hello_oper = {
.getattr = hello_getattr,
.readdir = hello_readdir,
.open = hello_open,
.read = hello_read,
};

int main(int argc, char *argv[])
{
return fuse_main(argc, argv, &hello_oper, NULL);
}



尽量不要修改struct fuse_operation.
希望大牛们不吝赐教。。因为感觉网上有关FUSE的资源确实太少。要么就是千篇一律的用hello.c,要么就是开源的文件系统,太过高深,不太适合我们学习。。拜谢,如果方便的话,提供下简单的例子,如果不太方便的话,提供下思路,以及需要修改的函数的片段。谢谢!!!
...全文
101 回复 打赏 收藏 转发到动态 举报
写回复
用AI写文章
回复
切换为时间正序
请发表友善的回复…
发表回复

69,373

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧