From d04af07dbed29f09bd5303e3f038f383d9ddab2b Mon Sep 17 00:00:00 2001 From: Yannick Ulrich Date: Sun, 19 Feb 2023 14:49:53 +0000 Subject: [PATCH] Implemented ReadDir --- fuse/main.go | 69 +++++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 68 insertions(+), 1 deletion(-) diff --git a/fuse/main.go b/fuse/main.go index 938ac04..c5e60c5 100644 --- a/fuse/main.go +++ b/fuse/main.go @@ -8,6 +8,7 @@ import ( "github.com/hanwen/go-fuse/v2/fs" "github.com/hanwen/go-fuse/v2/fuse" "strconv" + "strings" ) func (i *Device) HeartRateBytes() ([]byte, error) { @@ -46,6 +47,9 @@ type ITNode struct { fs.Inode kind int Ino uint64 + + lst []DirEntry + path string } var properties = []ITProperty { @@ -56,6 +60,8 @@ var properties = []ITProperty { ITProperty{"version", 6, nil}, ITProperty{"address", 7, nil}, } +var myfs *FS = nil; +var inodemap map[string]uint64 = nil; var _ = (fs.NodeReaddirer)((*ITNode)(nil)) @@ -89,6 +95,36 @@ func (n *ITNode) Readdir(ctx context.Context) (fs.DirStream, syscall.Errno) { } return fs.NewListDirStream(r), 0 + + case 2: + // on device + files, _ := myfs.ReadDir(n.path) + r := make([]fuse.DirEntry, len(files)) + n.lst = files + for ind, file := range files { + name := file.path[strings.LastIndex(file.path, "/")+1:] + + ino := inodemap[file.path] + if ino == 0 { + ino = uint64(len(inodemap)) + 1 + inodemap[file.path] = ino + } + + if file.flags & 0b1 == 1 { + r[ind] = fuse.DirEntry{ + Name: name, + Mode: fuse.S_IFDIR, + Ino : ino + 10, + } + } else { + r[ind] = fuse.DirEntry{ + Name: name, + Mode: fuse.S_IFREG, + Ino : ino + 10, + } + } + } + return fs.NewListDirStream(r), 0 } r := make([]fuse.DirEntry, 0) return fs.NewListDirStream(r), 0 @@ -112,7 +148,7 @@ func (n *ITNode) Lookup(ctx context.Context, name string, out *fuse.EntryOut) (* Mode: fuse.S_IFDIR, Ino: uint64(1), } - operations := &ITNode{kind: 2, Ino: 1} + operations := &ITNode{kind: 2, Ino: 1, path : ""} child := n.NewInode(ctx, operations, stable) return child, 0 } @@ -130,6 +166,34 @@ func (n *ITNode) Lookup(ctx context.Context, name string, out *fuse.EntryOut) (* } } return nil, syscall.ENOENT + + case 2: + // FS object + for _, file := range n.lst { + if file.path != n.path + "/" + name { + continue; + } + log.Println("matched", name) + + if file.flags & 0b1 == 1 { + stable := fs.StableAttr{ + Mode: fuse.S_IFDIR, + Ino: inodemap[file.path], + } + operations := &ITNode{kind: 2, path: file.path} + child := n.NewInode(ctx, operations, stable) + return child, 0 + } else { + stable := fs.StableAttr{ + Mode: fuse.S_IFREG, + Ino: inodemap[file.path], + } + operations := &ITNode{kind: 2, path: file.path} + child := n.NewInode(ctx, operations, stable) + return child, 0 + } + break; + } } return nil, syscall.ENOENT } @@ -202,6 +266,9 @@ func main() { log.Printf("Mounted on %s", mntDir) log.Printf("Unmount by calling 'fusermount -u %s'", mntDir) + myfs = &FS{}; + inodemap = make(map[string]uint64) + // Wait until unmount before exiting server.Wait() }