我使用fuse在麻省理工学院6.824实验室构建了自己的文件系统,并在此函数中实现了读取操作。
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);
}我无法通过读取(fd,readout,3)获取任何信息,但fuseserver_read打印的信息表明,缓冲区在fuse_reply_buf之前已被成功读出。
$ ./test
123,3
,0debug read in fuse: the content of 2 is 123, size 3那么为什么test.c中的read()不能从我的文件系统中读取任何东西呢?
发布于 2019-12-11 12:11:42
首先,我在编写测试文件时犯了错误。文件指针将指向“写”之后的文件末尾,当然以后什么也不能读取。因此,只需重新打开文件就可以使测试正常工作。其次,在FUSE的read()操作之前,FUSE将首先得到getattr(),并用文件的"size“属性截断read()操作的结果。因此,必须非常小心地操作文件的属性。
发布于 2019-11-15 09:29:29
还需要通过发送一个空缓冲区(作为"EOF“)通知您已经完成了阅读。您可以通过使用reply_buf_limited来做到这一点。看看hello_ll在 source tree中的例子
static void tfs_read(fuse_req_t req, fuse_ino_t ino, size_t size,
off_t off, struct fuse_file_info *fi) {
(void) fi;
assert(ino == FILE_INO);
reply_buf_limited(req, file_contents, file_size, off, size);
}
static int reply_buf_limited(fuse_req_t req, const char *buf, size_t bufsize,
off_t off, size_t maxsize)
{
if (off < bufsize)
return fuse_reply_buf(req, buf + off,
min(bufsize - off, maxsize));
else
return fuse_reply_buf(req, NULL, 0);
}https://stackoverflow.com/questions/58868933
复制相似问题