寻求FUSE目录包装器基础骨架公开实现:虚拟目录映射真实目录
FUSE 目录镜像包装器基础实现
下面是一个极简的FUSE基础骨架,实现虚拟挂载目录与真实目录的全操作同步——所有对挂载目录a/的文件/目录操作,都会直接映射到真实目录b/上,可作为二次开发的基础模板。
核心思路
所有FUSE回调函数中,将虚拟路径拼接为真实目录下的对应路径,直接调用系统原生文件操作函数完成实际逻辑,无需额外复杂处理。
基础代码实现
#define FUSE_USE_VERSION 30 #include <fuse.h> #include <stdio.h> #include <string.h> #include <unistd.h> #include <fcntl.h> #include <sys/stat.h> #include <dirent.h> #include <errno.h> #include <stdlib.h> // 定义真实目录路径,替换为你的实际路径 #define REAL_DIR "/path/to/b/" // 拼接虚拟路径到真实路径 static char* get_real_path(const char* path) { static char real_path[1024]; snprintf(real_path, sizeof(real_path), "%s%s", REAL_DIR, path + 1); // 去掉开头的/ return real_path; } // 获取文件/目录属性 static int mirror_getattr(const char* path, struct stat* stbuf) { int res; char* real_path = get_real_path(path); res = lstat(real_path, stbuf); if (res == -1) return -errno; return 0; } // 读取目录内容 static int mirror_readdir(const char* path, void* buf, fuse_fill_dir_t filler, off_t offset, struct fuse_file_info* fi) { DIR* dp; struct dirent* de; char* real_path = get_real_path(path); dp = opendir(real_path); if (dp == NULL) return -errno; while ((de = readdir(dp)) != NULL) { struct stat st; memset(&st, 0, sizeof(st)); st.st_ino = de->d_ino; st.st_mode = de->d_type << 12; if (filler(buf, de->d_name, &st, 0)) break; } closedir(dp); return 0; } // 创建目录 static int mirror_mkdir(const char* path, mode_t mode) { int res; char* real_path = get_real_path(path); res = mkdir(real_path, mode); if (res == -1) return -errno; return 0; } // 删除目录 static int mirror_rmdir(const char* path) { int res; char* real_path = get_real_path(path); res = rmdir(real_path); if (res == -1) return -errno; return 0; } // 创建文件 static int mirror_create(const char* path, mode_t mode, struct fuse_file_info* fi) { int res; char* real_path = get_real_path(path); res = open(real_path, fi->flags, mode); if (res == -1) return -errno; close(res); return 0; } // 删除文件 static int mirror_unlink(const char* path) { int res; char* real_path = get_real_path(path); res = unlink(real_path); if (res == -1) return -errno; return 0; } // 读取文件内容 static int mirror_read(const char* path, char* buf, size_t size, off_t offset, struct fuse_file_info* fi) { int fd; int res; char* real_path = get_real_path(path); fd = open(real_path, O_RDONLY); if (fd == -1) return -errno; res = pread(fd, buf, size, offset); if (res == -1) res = -errno; close(fd); return res; } // 写入文件内容 static int mirror_write(const char* path, const char* buf, size_t size, off_t offset, struct fuse_file_info* fi) { int fd; int res; char* real_path = get_real_path(path); fd = open(real_path, O_WRONLY); if (fd == -1) return -errno; res = pwrite(fd, buf, size, offset); if (res == -1) res = -errno; close(fd); return res; } // FUSE操作回调结构体 static struct fuse_operations mirror_oper = { .getattr = mirror_getattr, .readdir = mirror_readdir, .mkdir = mirror_mkdir, .rmdir = mirror_rmdir, .create = mirror_create, .unlink = mirror_unlink, .read = mirror_read, .write = mirror_write, }; int main(int argc, char* argv[]) { umask(0); return fuse_main(argc, argv, &mirror_oper, NULL); }
编译与运行
- 替换代码中
REAL_DIR的路径为你的真实目录b/的绝对路径 - 使用gcc编译(需提前安装fuse开发库,如
libfuse3-dev):gcc -o fuse_mirror fuse_mirror.c -lfuse3 - 创建虚拟挂载目录
a/,并执行挂载:mkdir -p /path/to/a/ ./fuse_mirror -o allow_other /path/to/a/ - 测试操作:在
a/下创建文件/目录,查看b/是否同步生成;修改a/x.txt,b/x.txt内容也会同步更新。
二次开发提示
- 可在回调函数中添加自定义逻辑(如权限校验、日志记录、操作拦截)
- 若需支持更多文件操作(如重命名、符号链接),补充对应的FUSE回调函数即可
内容的提问来源于stack exchange,提问作者rafoo
相关产品推荐
相关产品推荐

