Added FUSE support #55

Merged
Elara6331 merged 65 commits from yannickulrich/itd:fuse into master 2023-03-25 22:23:52 +00:00
2 changed files with 57 additions and 27 deletions
Showing only changes of commit 83726c9427 - Show all commits

View File

@ -8,7 +8,6 @@ import (
"github.com/hanwen/go-fuse/v2/fs" "github.com/hanwen/go-fuse/v2/fs"
"github.com/hanwen/go-fuse/v2/fuse" "github.com/hanwen/go-fuse/v2/fuse"
"strconv" "strconv"
"strings"
) )
func (i *Device) HeartRateBytes() ([]byte, error) { func (i *Device) HeartRateBytes() ([]byte, error) {
@ -43,6 +42,14 @@ type ITProperty struct {
f func() ([]byte, error) f func() ([]byte, error)
} }
type DirEntry struct {
isDir bool
modtime uint64
size uint32
path string
}
type ITNode struct { type ITNode struct {
fs.Inode fs.Inode
kind int kind int
@ -102,9 +109,18 @@ func (n *ITNode) Readdir(ctx context.Context) (fs.DirStream, syscall.Errno) {
files, _ := myfs.ReadDir(n.path) files, _ := myfs.ReadDir(n.path)
log.Info("readdir").Str("path", n.path).Int("objects", len(files)).Send() log.Info("readdir").Str("path", n.path).Int("objects", len(files)).Send()
r := make([]fuse.DirEntry, len(files)) r := make([]fuse.DirEntry, len(files))
n.lst = files n.lst = make([]DirEntry, len(files))
for ind, file := range files { for ind, entry := range files {
name := file.path[strings.LastIndex(file.path, "/")+1:] info, _ := entry.Info()
name := info.Name()
file := DirEntry{
path: n.path + "/" + name,
size: uint32(info.Size()),
modtime: uint64(info.ModTime().Unix()),
isDir: info.IsDir(),
}
n.lst[ind] = file
ino := inodemap[file.path] ino := inodemap[file.path]
if ino == 0 { if ino == 0 {
@ -112,7 +128,7 @@ func (n *ITNode) Readdir(ctx context.Context) (fs.DirStream, syscall.Errno) {
inodemap[file.path] = ino inodemap[file.path] = ino
} }
if file.flags & 0b1 == 1 { if file.isDir {
r[ind] = fuse.DirEntry{ r[ind] = fuse.DirEntry{
Name: name, Name: name,
Mode: fuse.S_IFDIR, Mode: fuse.S_IFDIR,
@ -177,7 +193,7 @@ func (n *ITNode) Lookup(ctx context.Context, name string, out *fuse.EntryOut) (*
} }
log.Info("LookUp successful").Str("path", file.path).Send() log.Info("LookUp successful").Str("path", file.path).Send()
if file.flags & 0b1 == 1 { if file.isDir {
stable := fs.StableAttr{ stable := fs.StableAttr{
Mode: fuse.S_IFDIR, Mode: fuse.S_IFDIR,
Ino: inodemap[file.path], Ino: inodemap[file.path],
@ -483,7 +499,7 @@ func main() {
Str("target", mntDir). Str("target", mntDir).
Err(err). Err(err).
Send() Send()
return err return
} }
dev := Device{}; dev := Device{};

View File

@ -3,6 +3,7 @@ import (
"os" "os"
"io/ioutil" "io/ioutil"
"errors" "errors"
"time"
) )
const ( const (
@ -26,12 +27,30 @@ type Device struct {
type FS struct { type FS struct {
} }
type DirEntry struct { type MyFileInfo struct {
status int8 name string
flags uint32
modtime uint64
size uint32 size uint32
path string modtime time.Time
dir bool
}
func (n *MyFileInfo) Name() string {
return n.name
}
func (n *MyFileInfo) Size() uint32 {
return n.size
}
func (n *MyFileInfo) ModTime() time.Time {
return n.modtime
}
func (n *MyFileInfo) IsDir() bool {
return n.dir
}
type MyDirEntry struct {
self MyFileInfo
}
func (n *MyDirEntry) Info() (MyFileInfo, error) {
return n.self, nil
} }
type File struct { type File struct {
@ -74,8 +93,8 @@ func (i *Device) Version() (string, error) {
return "1.11.0", nil return "1.11.0", nil
} }
func (blefs *FS) ReadDir(path string) ([]DirEntry, error) { func (blefs *FS) ReadDir(path string) ([]MyDirEntry, error) {
var out []DirEntry var out []MyDirEntry
files, err := ioutil.ReadDir("./root/" + path) files, err := ioutil.ReadDir("./root/" + path)
if err != nil { if err != nil {
@ -83,17 +102,12 @@ func (blefs *FS) ReadDir(path string) ([]DirEntry, error) {
} }
for _, f := range files { for _, f := range files {
listing := DirEntry{} listing := MyFileInfo{}
listing.status = FSStatusOk listing.modtime = f.ModTime()
listing.modtime = uint64(f.ModTime().Unix())
listing.size = uint32(f.Size()) listing.size = uint32(f.Size())
listing.path = path + "/" + f.Name() listing.name = path + "/" + f.Name()
if f.IsDir() { listing.dir = f.IsDir()
listing.flags = 1; out = append(out, MyDirEntry{self : listing})
} else {
listing.flags = 0;
}
out = append(out, listing)
} }
return out, nil return out, nil
} }