结构体 Inode
StaleAttr 包括权限,编号,主要结构在 rawBridge
注意:生成 Inode 使用 NewInode 或者 NewPersistentInode
// Inode is a node in VFS tree. Inodes are one-to-one mapped to // Operations instances, which is the extension interface for file // systems. One can create fully-formed trees of Inodes ahead of time // by creating "persistent" Inodes. // // The Inode struct contains a lock, so it should not be // copied. Inodes should be obtained by calling Inode.NewInode() or // Inode.NewPersistentInode(). type Inode struct { stableAttr StableAttr ops InodeEmbedder bridge *rawBridge // Following data is mutable. // file handles. // protected by bridge.mu openFiles []uint32
1. Mount 函数
Mount 函数在目录 dir 下,挂载指定文件系统 NodeFS,开启服务请求
如果未指定选项参数,则设置默认参数
// Mount mounts the given NodeFS on the directory, and starts serving
// requests. This is a convenience wrapper around NewNodeFS and
// fuse.NewServer. If nil is given as options, default settings are
// applied, which are 1 second entry and attribute timeout.
func Mount(dir string, root InodeEmbedder, options *Options) (*fuse.Server, error) {
if options == nil {
oneSec := time.Second
options = &Options{
EntryTimeout: &oneSec,
AttrTimeout: &oneSec,
}
}
1.1 NewNodeFS 函数
使用了 bridge 变量定义 raw 文件系统,会不会学网络 bridge 概念,实例化,并设置参数
如果 root 实现了 NodeOnAdder 接口,则调用 OnAdd 方法
// NewNodeFS creates a node based filesystem based on the
// InodeEmbedder instance for the root of the tree.
func NewNodeFS(root InodeEmbedder, opts *Options) fuse.RawFileSystem {
initInode(root.embed(), root,
StableAttr{
Ino: 1,
Mode: fuse.S_IFDIR,
},
bridge,
false,
)
bridge.root = root.embed()
bridge.root.lookupCount = 1
bridge.nodes = map[uint64]*Inode{
1: bridge.root,
}
// Fh 0 means no file handle.
bridge.files = []*fileEntry{{}}
if opts.OnAdd != nil {
opts.OnAdd(context.Background())
} else if oa, ok := root.(NodeOnAdder); ok {
oa.OnAdd(context.Background())
}
return bridge
}
1.2 NewServer 函数
实例化 rawBridge 对象,实现了 RawFileSystem 接口,包括处理文件属性,读写等等ri
type RawFileSystem interface {
// File handling.
Create(cancel <-chan struct{}, input *CreateIn, name string, out *CreateOut) (code Status)
Open(cancel <-chan struct{}, input *OpenIn, out *OpenOut) (status Status)
Read(cancel <-chan struct{}, input *ReadIn, buf []byte) (ReadResult, Status)
Lseek(cancel <-chan struct{}, in *LseekIn, out *LseekOut) Status
// Directory handling
OpenDir(cancel <-chan struct{}, input *OpenIn, out *OpenOut) (status Status)
ReadDir(cancel <-chan struct{}, input *ReadIn, out *DirEntryList) Status
ReadDirPlus(cancel <-chan struct{}, input *ReadIn, out *DirEntryList) Status
ReleaseDir(input *ReleaseIn)
FsyncDir(cancel <-chan struct{}, input *FsyncIn) (code Status)
调用 mount 函数,其实是调用 fusermount 命令将文件系统挂载在设置的目录