把 Galgame 做成文件系统
原片 01:18:01—01:19:33
每个目录、文件的读写都由自己决定,理论上也能在 Windows 上做 proc。这里用上节的 Galgame,做成 ggfs 文件系统,底层用 fuse。

这里是 fuse version 26,实现 struct fuse_operations,定义 open、read 等操作。

ggfs.c 的操作表片段;右侧各回调函数在文件其他位置定义:
static struct fuse_operations ggfs_ops = {
.getattr = ggfs_getattr,
.readdir = ggfs_readdir,
.open = ggfs_open,
.read = ggfs_read,
.write = ggfs_write,
.truncate = ggfs_truncate,
.statfs = ggfs_statfs,
.create = ggfs_create,
.mkdir = ggfs_mkdir,
.unlink = ggfs_unlink,
.rename = ggfs_rename,
};
readdir 想返回什么目录项就返回什么,读取文件时也能提供任意数据。代码是 turing complete,理论上可以做任何事。

ggfs_readdir 中添加当前场景文件的片段;sc、buf、filler 和锁的上下文来自原函数:
filler(buf, ".", NULL, 0);
filler(buf, "..", NULL, 0);
for (int i = 0; i < sc->num_choices; i++)
filler(buf, sc->choices[i].filename, NULL, 0);
if (sc->image[0])
filler(buf, ".scene.jpg", NULL, 0);
if (sc->description[0])
filler(buf, ".description", NULL, 0);
if (sc->end_title[0])
filler(buf, ".ending", NULL, 0);
这次不用预建很多链接,/tmp 下 ggfs 起初是空的。make fuse 后进入 galgame 模式,mount 能看到 ggfs 已挂到 /tmp 下的 ggfs。