熔丝文件系统问题
我正在开发一个将数据存储在 RAM 中的保险丝文件系统,但是当我向文件写入内容时遇到问题。文件变成空白文件。
这是代码:
#define FUSE_USE_VERSION 26
#include <fuse/fuse.h>
#include <string.h>
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
char hello_data[200] = {'h', 'e', 'l', 'l', 'o', '\n'};
int hello_size = 6;
int homsifs_getattr(const char* path, struct stat* stbuf)
{
memset(stbuf, 0, sizeof(struct stat));
if (strcmp(path, "/") == 0)
stbuf->st_mode = S_IFDIR | 0666;
else if (strcmp(path, "/hello") == 0)
{
stbuf->st_mode = S_IFREG | 0666;
stbuf->st_size = hello_size;
}
else
return -ENOENT;
return 0;
}
int homsifs_readdir(const char* path, void* buf, fuse_fill_dir_t filler,
off_t offset, struct fuse_file_info* fi)
{
if (strcmp(path, "/") != 0)
return -ENOENT;
filler(buf, ".", NULL, 0);
filler(buf, "..", NULL, 0);
filler(buf, "hello", NULL, 0);
return 0;
}
int homsifs_open(const char* path, struct fuse_file_info* fi)
{
if (strcmp(path, "/hello") != 0)
return -ENOENT;
return 0;
}
int homsifs_read(const char* path, char* buf, size_t size, off_t offset,
struct fuse_file_info* fi)
{
int offset_size = hello_size - offset;
if (size > offset_size)
size = offset_size;
memcpy(buf, hello_data + offset, size);
return size;
}
int homsifs_truncate(const char* path, off_t size)
{
if (size > 200)
return -ENOMEM;
if (hello_size < size)
memset(hello_data + size, 0, size - hello_size);
hello_size = size;
return 0;
}
int homsifs_write(const char* path, const char* buf, size_t size, off_t offset,
struct fuse_file_info* fi)
{
if (strcmp(path, "/hello") != 0)
return -ENOENT;
memcpy(hello_data + offset, buf, size);
return size;
}
struct fuse_operations homsifs_operations =
{
.getattr = homsifs_getattr,
.readdir = homsifs_readdir,
.open = homsifs_open,
.read = homsifs_read,
.truncate = homsifs_truncate,
.write = homsifs_write
};
int main(int argc, char** argv)
{
return fuse_main(argc, argv, &homsifs_operations, NULL);
}
有人知道出了什么问题吗?
谢谢。
i'm developing a fuse filesystem that stores data in the RAM, but i'm having problems when i write something to a file. The file turns into a blank file.
Here's the code:
#define FUSE_USE_VERSION 26
#include <fuse/fuse.h>
#include <string.h>
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
char hello_data[200] = {'h', 'e', 'l', 'l', 'o', '\n'};
int hello_size = 6;
int homsifs_getattr(const char* path, struct stat* stbuf)
{
memset(stbuf, 0, sizeof(struct stat));
if (strcmp(path, "/") == 0)
stbuf->st_mode = S_IFDIR | 0666;
else if (strcmp(path, "/hello") == 0)
{
stbuf->st_mode = S_IFREG | 0666;
stbuf->st_size = hello_size;
}
else
return -ENOENT;
return 0;
}
int homsifs_readdir(const char* path, void* buf, fuse_fill_dir_t filler,
off_t offset, struct fuse_file_info* fi)
{
if (strcmp(path, "/") != 0)
return -ENOENT;
filler(buf, ".", NULL, 0);
filler(buf, "..", NULL, 0);
filler(buf, "hello", NULL, 0);
return 0;
}
int homsifs_open(const char* path, struct fuse_file_info* fi)
{
if (strcmp(path, "/hello") != 0)
return -ENOENT;
return 0;
}
int homsifs_read(const char* path, char* buf, size_t size, off_t offset,
struct fuse_file_info* fi)
{
int offset_size = hello_size - offset;
if (size > offset_size)
size = offset_size;
memcpy(buf, hello_data + offset, size);
return size;
}
int homsifs_truncate(const char* path, off_t size)
{
if (size > 200)
return -ENOMEM;
if (hello_size < size)
memset(hello_data + size, 0, size - hello_size);
hello_size = size;
return 0;
}
int homsifs_write(const char* path, const char* buf, size_t size, off_t offset,
struct fuse_file_info* fi)
{
if (strcmp(path, "/hello") != 0)
return -ENOENT;
memcpy(hello_data + offset, buf, size);
return size;
}
struct fuse_operations homsifs_operations =
{
.getattr = homsifs_getattr,
.readdir = homsifs_readdir,
.open = homsifs_open,
.read = homsifs_read,
.truncate = homsifs_truncate,
.write = homsifs_write
};
int main(int argc, char** argv)
{
return fuse_main(argc, argv, &homsifs_operations, NULL);
}
Anyone knows what's wrong?
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
当程序打开文件进行写入时,它会被截断。此后,写入的任何数据都将无法访问,因为您没有正确更新
homsifs_write
中的hello_size
。When a program opens the file for writing, it gets truncated. After that, any data written is inaccessible because you're not correctly updating
hello_size
inhomsifs_write
.