2023-02-19 11:50:02 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"log"
|
|
|
|
"os"
|
|
|
|
"context"
|
|
|
|
"syscall"
|
|
|
|
"github.com/hanwen/go-fuse/v2/fs"
|
|
|
|
"github.com/hanwen/go-fuse/v2/fuse"
|
|
|
|
)
|
|
|
|
|
|
|
|
type ITNode struct {
|
|
|
|
fs.Inode
|
|
|
|
kind int
|
|
|
|
}
|
|
|
|
|
|
|
|
var _ = (fs.NodeReaddirer)((*ITNode)(nil))
|
|
|
|
|
|
|
|
// Readdir is part of the NodeReaddirer interface
|
|
|
|
func (n *ITNode) Readdir(ctx context.Context) (fs.DirStream, syscall.Errno) {
|
2023-02-19 12:08:45 +00:00
|
|
|
switch n.kind {
|
|
|
|
case 0:
|
2023-02-19 11:50:02 +00:00
|
|
|
// root folder
|
|
|
|
r := make([]fuse.DirEntry, 2)
|
|
|
|
r[0] = fuse.DirEntry{
|
|
|
|
Name: "device",
|
|
|
|
Ino: 0,
|
|
|
|
Mode: fuse.S_IFDIR,
|
|
|
|
}
|
|
|
|
r[1] = fuse.DirEntry{
|
|
|
|
Name: "fs",
|
|
|
|
Ino: 1,
|
|
|
|
Mode: fuse.S_IFDIR,
|
|
|
|
}
|
|
|
|
return fs.NewListDirStream(r), 0
|
|
|
|
}
|
|
|
|
r := make([]fuse.DirEntry, 0)
|
|
|
|
return fs.NewListDirStream(r), 0
|
|
|
|
}
|
|
|
|
|
|
|
|
var _ = (fs.NodeLookuper)((*ITNode)(nil))
|
|
|
|
func (n *ITNode) Lookup(ctx context.Context, name string, out *fuse.EntryOut) (*fs.Inode, syscall.Errno) {
|
2023-02-19 12:08:45 +00:00
|
|
|
switch n.kind {
|
|
|
|
case 0:
|
|
|
|
// root folder
|
2023-02-19 11:50:02 +00:00
|
|
|
if name == "device" {
|
|
|
|
stable := fs.StableAttr{
|
|
|
|
Mode: fuse.S_IFDIR,
|
|
|
|
Ino: uint64(0),
|
|
|
|
}
|
|
|
|
operations := &ITNode{kind: 1}
|
|
|
|
child := n.NewInode(ctx, operations, stable)
|
|
|
|
return child, 0
|
|
|
|
} else if name == "fs" {
|
|
|
|
stable := fs.StableAttr{
|
|
|
|
Mode: fuse.S_IFDIR,
|
|
|
|
Ino: uint64(1),
|
|
|
|
}
|
|
|
|
operations := &ITNode{kind: 2}
|
|
|
|
child := n.NewInode(ctx, operations, stable)
|
|
|
|
return child, 0
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil, syscall.ENOENT
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func main() {
|
|
|
|
// This is where we'll mount the FS
|
|
|
|
mntDir := "/tmp/x"
|
|
|
|
os.Mkdir(mntDir, 0755)
|
|
|
|
root := &ITNode{kind: 0}
|
|
|
|
server, err := fs.Mount(mntDir, root, &fs.Options{
|
|
|
|
MountOptions: fuse.MountOptions{
|
|
|
|
// Set to true to see how the file system works.
|
|
|
|
Debug: false,
|
|
|
|
},
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
log.Panic(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
log.Printf("Mounted on %s", mntDir)
|
|
|
|
log.Printf("Unmount by calling 'fusermount -u %s'", mntDir)
|
|
|
|
|
|
|
|
// Wait until unmount before exiting
|
|
|
|
server.Wait()
|
|
|
|
}
|