一、libfuse3
libfuse3是一个用户空间的文件系统框架,它允许开发人员在Linux上实现不同的虚拟文件系统。它是libfuse的升级版本,相比之下有更好的性能和更好的稳定性,并提供新的功能和API。
#include <fuse3/fuse.h>
static int hello_getattr(const char *path, struct stat *stbuf, struct fuse_file_info *fi)
{
int res = 0;
memset(stbuf, 0, sizeof(struct stat));
if (strcmp(path, "/") == 0) {
stbuf->st_mode = S_IFDIR | 0755;
stbuf->st_nlink = 2;
} else if (strcmp(path, "/hello") == 0) {
stbuf->st_mode = S_IFREG | 0444;
stbuf->st_nlink = 1;
stbuf->st_size = strlen(hello_str);
} else
res = -ENOENT;
return res;
}
static int hello_readdir(const char *path, void *buf, fuse_fill_dir_t filler,
off_t offset, struct fuse_file_info *fi, enum fuse_readdir_flags flags)
{
if (strcmp(path, "/") != 0)
return -ENOENT;
filler(buf, ".", NULL, 0, FUSE_FILL_DIR_PLUS);
filler(buf, "..", NULL, 0, FUSE_FILL_DIR_PLUS);
filler(buf, "hello", NULL, 0, FUSE_FILL_DIR_PLUS);
return 0;
}
static int hello_open(const char *path, struct fuse_file_info *fi)
{
if (strcmp(path, "/hello") != 0)
return -ENOENT;
if ((fi->flags & 3) != O_RDONLY)
return -EACCES;
return 0;
}
static int hello_read(const char *path, char *buf, size_t size, off_t offset,
struct fuse_file_info *fi)
{
size_t len;
(void) fi;
if(strcmp(path,"/hello") != 0)
return -ENOENT;
len = strlen(hello_str);
if (offset len)
size = len - offset;
memcpy(buf, hello_str + offset, size);
} else
size = 0;
return size;
}
static struct fuse_operations hello_oper = {
.getattr = hello_getattr,
.readdir = hello_readdir,
.open = hello_open,
.read = hello_read,
};
int main(int argc, char *argv[])
{
return fuse_main(argc, argv, &hello_oper, NULL);
}二、libfuse.so.2 is needed by
如果编译时出现类似”libfuse.so.2 is needed by”的错误,则需要安装fuse-devel(如CentOS)或libfuse-dev(如Ubuntu)。
三、libfuse.so
如果编译时出现类似”libfuse.so: undefined reference to”的错误信息,可能是由于链接器无法找到正确版本的libfuse库(.so文件)。可以通过将包含libfuse库的目录添加到LD_LIBRARY_PATH环境变量中,或者使用-L选项指定libfuse库所在的目录。
四、libfuse使用
使用libfuse开发用户空间文件系统需要实现一组回调函数,这些回调函数将负责实现用户空间文件系统的行为,如获取文件属性、读写文件和目录遍历等等。libfuse将这些回调函数打包到一个结构体中,然后在mount操作中注册这个结构体,以将用户空间的文件系统挂载到Linux文件系统中。
以下是使用libfuse实现简单用户空间文件系统的示例:
#include <fuse.h>
#include <stdio.h>
#include <string.h>
#include <errno.h>
#include <fcntl.h>
static const char *hello_str = "Hello World!\n";
static const char *hello_path = "/hello";
static int hello_getattr(const char *path, struct stat *stbuf)
{
int res = 0;
memset(stbuf, 0, sizeof(struct stat));
if (strcmp(path, "/") == 0) {
stbuf->st_mode = S_IFDIR | 0755;
stbuf->st_nlink = 2;
} else if (strcmp(path, hello_path) == 0) {
stbuf->st_mode = S_IFREG | 0444;
stbuf->st_nlink = 1;
stbuf->st_size = strlen(hello_str);
} else
res = -ENOENT;
return res;
}
static int hello_open(const char *path, struct fuse_file_info *fi)
{
if (strcmp(path, hello_path) != 0)
return -ENOENT;
if ((fi->flags & 3) != O_RDONLY)
return -EACCES;
return 0;
}
static int hello_read(const char *path, char *buf, size_t size, off_t offset, struct fuse_file_info *fi)
{
size_t len;
(void) fi;
if(strcmp(path,hello_path) != 0)
return -ENOENT;
len = strlen(hello_str);
if (offset len)
size = len - offset;
memcpy(buf, hello_str + offset, size);
} else
size = 0;
return size;
}
static struct fuse_operations hello_oper = {
.getattr = hello_getattr,
.open = hello_open,
.read = hello_read,
};
int main(int argc, char *argv[])
{
return fuse_main(argc, argv, &hello_oper, NULL);
}五、libfuse写优化 拷贝内存
在使用libfuse编写用户空间文件系统时,需要注意减少拷贝内存的次数。为了尽可能减少数据拷贝,可以使用Linux的sendfile系统调用和splice系统调用来减少进程间数据移动(例如从网络套接字到用户空间缓冲区)。而应使用read和write系统调用,不应使用fread和fwrite等高级I / O函数。
六、libfuse arm linux
libfuse可用于嵌入式Linux ARM平台。开发人员需要确保ARM编译器和libfuse库都可用,并且需要导出以下环境变量才能正确引用库:
export PKG_CONFIG_SYSROOT_DIR=path/to/sysroot
export PKG_CONFIG_PATH=path/to/pkgconfig/folder
export ACLOCAL_PATH=path/to/aclocal/folder七、libfuse-dev 找不到
如果在使用Ubuntu时尝试安装libfuse-dev并出现”无法找到libfuse-dev”的错误消息,您可以安装libfuse-dev包的替代方法是使用pkg-config工具和fuse.pc文件安装libfuse的开发。可以使用以下命令来安装:
sudo apt-get install pkg-config
pkg-config --list-all | grep fuse
sudo apt-get install libfuse3-dev原创文章,作者:小蓝,如若转载,请注明出处:https://www.506064.com/n/151060.html
微信扫一扫
支付宝扫一扫