无法通过 FUSE 文件系统"read"任何内容
Can not "read" anything through the FUSE file system
我在MIT 6.824实验室中使用fuse构建了自己的文件系统,read操作就是在这个函数中实现的。
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 %lun",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返回之前打印它的信息。
当然,还实现了write操作。
然后我做了一个简单的测试来读出一些单词。
//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,%dn",buf,writesize);
printf("%s,%dn",readout,readsize);
close(fd);
}
通过读取(fd,readout,3(,我什么也得不到,但fuseserver_read
打印的信息显示,在fuse_reply_buf
之前,缓冲区已成功读取
$ ./test
123,3
,0
debug read in fuse: the content of 2 is 123, size 3
那么,为什么test.c中的read()
不能从我的文件系统中读取任何内容呢??
首先,我在编写测试文件时犯了一个错误。文件指针将指向"写入"后的文件末尾,当然以后什么也读不到。因此,只需重新打开文件就可以使测试正常进行。其次,在FUSE的read((操作之前,FUSE将首先获取attr((,并用文件的"size"属性截断read(((操作的结果。因此,操作文件的属性必须非常小心。
还需要通过发送一个空缓冲区(作为"EOF"(来通知您已完成读取。您可以使用reply_buf_limited
来完成此操作。看看保险丝源树中的hello_ll
示例:
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);
}
相关文章:
- 是否有任何内置的哈希图函数来处理 c++ 中的值输入?
- 无法通过 FUSE 文件系统"read"任何内容
- std::任何只用于移动的模板,其中副本ctor内的static_assert等于编译错误,但为什么
- 是否有任何内置函数可以检查给定的两个数字在给定整数数组中的顺序是否相同?
- std::ifstream.read() 不会向我的缓冲区返回任何内容
- "... already defined in *.obj"但我没有在同一范围内两次定义任何变量
- C++ 中是否有任何内置阶乘函数?
- 如何在C++中接收任何类型的函数参数并获取函数内传递变量的类型?
- 是否有任何内置函数可以告诉编译器分支是否可预测
- 调用向量内的函数指针不执行任何操作
- 我需要在C /C#中使用图形.我可以使用任何内置的C /C#库
- 是否有任何内置类可以处理C 中的大整数值
- c ++中是否有任何内置功能可以在计划的时间运行任务
- 是否有任何内置的 UTF 在 c++ 在窗口中
- 在VC++2010中执行任何内置的ARRAY函数
- C++:如何在不使用任何内置函数的情况下将字符串转换为整数
- 是否有任何内置的CUDA函数允许CUDA内核向主机代码报告错误?
- 我如何在常数时间(O(1))内获得字母表(1-26)中字符的数值/位置,而不使用任何内置方法或函数
- C++中的任何内置哈希方法
- c++库中的任何内置夹子方法