首页 > 解决方案 > 无法通过 FUSE 文件系统“读取”任何内容

问题描述

我在MIT 6.824实验室使用fuse搭建了自己的文件系统,在这个函数中实现了操作。

void
fuseserver_read(fuse_req_t req, fuse_ino_t ino, size_t size,
        off_t off, struct fuse_file_info *fi)
{
    std::string buf;
    int r;
    if ((r = yfs->read(ino, size, off, buf)) == yfs_client::OK) {

        char* retbuf = (char *)malloc(buf.size());
        memcpy(retbuf,buf.data(),buf.size());
        //Print the information of the result.
        printf("debug read in fuse: the content of %lu is %s, size %lu\n",ino,retbuf, buf.size());

       fuse_reply_buf(req,retbuf,buf.size());    
    } else {
        fuse_reply_err(req, ENOENT);
    }

//global definition
//struct fuse_lowlevel_ops fuseserver_oper;

//In main()
//    fuseserver_oper.read       = fuseserver_read;

我在它返回之前打印 buf 的信息。

当然也实现了写操作。

然后我运行一个简单的测试来读出一些单词。

//test.c
int main(){
    //./yfs1 is the mount point of my filesystem
    int fd = open("./yfs1/test-file",O_RDWR | O_CREAT,0777);
    char* buf = "123";
    char* readout;
    readout = (char *)malloc(3);
    int writesize = write(fd,buf,3);
    int readsize = read(fd,readout,3);
    printf("%s,%d\n",buf,writesize);
    printf("%s,%d\n",readout,readsize);
    close(fd);
}

我通过read(fd,readout,3)什么也得不到,但是打印出来的信息fuseserver_read显示缓冲区之前已经成功读出fuse_reply_buf

$ ./test
123,3
,0
debug read in fuse: the content of 2 is 123, size 3

那么为什么read()in test.c 不能从我的文件系统中读取任何内容呢?

标签: c++filesystemsfuse

解决方案


首先,我在编写测试文件时犯了一个错误。文件指针会在“写”之后指向文件的结尾,当然以后什么也不能读。所以只需重新打开文件就可以使测试工作。其次,在 FUSE 的 read() 操作之前,FUSE 会先 getattr() 并用文件的“size”属性截断 read() 操作的结果。所以必须非常小心地操作文件的属性。


推荐阅读