gpt4 book ai didi

c - fuse 更改为?

转载 作者:太空宇宙 更新时间:2023-11-04 04:04:14 27 4
gpt4 key购买 nike

我正在尝试修改 fuse example挂载任何目录。我想在 tmp 中挂载/home/nikhil。我运行它,$ ./ni/home/nikhil tmp

它挂载了 tmp 文件夹,但无法访问它。

$ls -ltr tmp 

ls:无法访问 tmp:不允许操作

$ ls -ltr

ls: 无法访问 delete: 不允许操作
总计 11716
d???????? ? ? ? ? ?删除

我的代码是

#define FUSE_USE_VERSION 26

#include <fuse.h>
#include <stdio.h>
#include <string.h>
#include <errno.h>
#include <fcntl.h>
#include <stdlib.h>
#include <limits.h>
#include <unistd.h>

#define MAX 100
char *rootpath;

static void ni_fullpath(char fpath[MAX], const char *path){
strcpy(fpath, rootpath);
strncat(fpath, path, MAX);
}

static int ni_getattr(const char *path, struct stat *stbuf)
{
int res = 0;
char fpath[MAX];
memset(stbuf, 0, sizeof(struct stat));
ni_fullpath(fpath, path);
res = lstat(fpath, stbuf);
return res;
}

static int ni_readdir(const char *path, void *buf, fuse_fill_dir_t filler,
off_t offset, struct fuse_file_info *fi)
{
(void) offset;
(void) fi;
// i didnt understand this
filler(buf, ".", NULL, 0);
filler(buf, "..", NULL, 0);
ni_fullpath(fpath, path);
filler(buf, fpath + 1, NULL, 0);

return 0;
}

static int ni_open(const char *path, struct fuse_file_info *fi)
{
int fd;
char fpath[MAX];
ni_fullpath(fpath, path);
if ((fi->flags & 3) != O_RDONLY)
return -EACCES;

fd = open(fpath, fi->flags);
return fd;
}

static int ni_read(const char *path, char *buf, size_t size, off_t offset,
struct fuse_file_info *fi)
{
return pread(fi->fh, buf, size, offset);

}

static struct fuse_operations ni_oper = {
.getattr = ni_getattr,
.readdir = ni_readdir,
.open = ni_open,
.read = ni_read,
};


void ni_usage(){
fprintf(stderr, "usage ni rootDir mountPoint");
abort();
}

int main(int argc, char *argv[])
{
printf("%s %s \n", argv[1], argv[2]);
rootpath = realpath(argv[1], NULL);

argv[1] = argv[2];
argc--;
return fuse_main(argc, argv, &ni_oper, NULL);
}

任何人都可以帮助我做错什么吗?我正在使用 ubuntu 1104 64 位。

最佳答案

使用未初始化的变量 fpath 代替 path 怎么样?

static int ni_getattr(const char *path, struct stat *stbuf)
{
int res = 0;
char fpath[MAX];
memset(stbuf, 0, sizeof(struct stat));

res = lstat(fpath, stbuf);
return res;
}

您可能错过了 ni_fullpath(fpath, path);

据我所知,如果成功,应该在 open 回调中返回 0,所以它应该是这样的:

   ....
fd = open(fpath, fi->flags);
if (fd < 0)
return -errno;
fi->fh = fd;
return 0;
}

列表操作应该使用 readdir 回调,在你的情况下它的应用非常有限。最好在fusexmp的基础上起码.检查那里的 readdir 是如何实现的。

关于c - fuse 更改为?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8097896/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com