Gau*_*tam 12 linux filesystems fuse
编写一个存储在内存中的简单FUSE文件系统.文件系统必须支持以下命令:
ls,mkdir,cp
这个问题最近在接受采访时被问到,我无法回答.所以我决定学习它.
做了一些搜索,找到了一些关于构建我自己的FUSE文件系统的指南.我真的对如何在内存中实现文件系统毫无头绪.
我的问题是
我正在阅读的链接:
在最后一个链接中,提到了使用PyFileSystem进行内存中缓存.我不确定这可能会有什么帮助.
PS:这是一个书面采访问题,所以答案必须足够简单,在10-15分钟内写在纸上.
您没有指定编程语言,尽管 FUSE 是本机 C++,但有本机 Golang 绑定,在bazil.org/fuse实现。
我想说答案的主要部分需要包括以下内容:
我最近使用此适配器编写了一个内存文件系统:github.com/bbengfort/memfs。我关于其性能的文章在这里:带有 FUSE 的内存文件系统。很快,我做出了一些选择:
内存数据结构包含 2 个主要结构,dir 和 file,它们都是节点:
type Node struct {
ID uint64
Name string
Attrs fuse.Attr
Parent *Dir
}
type Dir struct {
Node
Children map[string]Node
}
type File struct {
Node
Data []byte
}
Run Code Online (Sandbox Code Playgroud)
Children正如您所看到的,这是一个简单的树,可以通过和链接上下遍历Parent。File的属性Data保存了文件的所有内容。因此,文件系统只需要创建一个"\"在挂载点调用的“根”目录,然后将 on mkdiraDir添加到其子目录,然后添加on cpa 。File在 Go 中,这很简单:
type FS struct {
root *Dir
}
func Mount(path string) error {
// Unmount the FS in case it was mounted with errors.
fuse.Unmount(path)
// Mount the FS with the specified options
conn, err := fuse.Mount(path)
if err != nil {
return err
}
// Ensure that the file system is shutdown
defer conn.Close()
// Create the root dir and file system
memfs := FS{
root: &Dir{
ID: 1,
Name: "\",
Parent: nil,
},
}
// Serve the file system
if err := fs.Serve(conn, memfs); err != nil {
return err
}
}
Run Code Online (Sandbox Code Playgroud)
现在您需要钩子来实现各种 FUSE 请求和调用。这是一个示例mkdir:
func (d *Dir) Mkdir(ctx context.Context, req *fuse.MkdirRequest) (fs.Node, error) {
// Update the directory Atime
d.Attrs.Atime = time.Now()
// Create the child directory
c := new(Dir)
c.Init(req.Name, req.Mode, d)
// Set the directory's UID and GID to that of the caller
c.Attrs.Uid = req.Header.Uid
c.Attrs.Gid = req.Header.Gid
// Add the directory to the directory
d.Children[c.Name] = c
// Update the directory Mtime
d.Attrs.Mtime = time.Now()
return c, nil
}
Run Code Online (Sandbox Code Playgroud)
最后,通过讨论如何编译和运行服务器、安装到路径以及 FUSE 如何拦截内核调用并将它们传递到用户空间中的进程来结束面试问题。