Run formatter

This commit is contained in:
Elara 2023-03-25 15:24:46 -07:00
parent 9ecd45dadd
commit ee4f563b05
5 changed files with 103 additions and 72 deletions

View File

@ -107,7 +107,7 @@ func setCfgDefaults() {
"music.vol.interval": 5,
"fuse.enabled": false,
"fuse.enabled": false,
"fuse.mountpoint": "/tmp/itd/mnt",
}, "."), nil)
}

15
fuse.go
View File

@ -1,17 +1,19 @@
package main
import (
"go.arsenm.dev/itd/internal/fusefs"
"context"
"os"
"github.com/hanwen/go-fuse/v2/fs"
"github.com/hanwen/go-fuse/v2/fuse"
"go.arsenm.dev/logger/log"
"context"
"go.arsenm.dev/infinitime"
"go.arsenm.dev/itd/internal/fusefs"
"go.arsenm.dev/logger/log"
)
func startFUSE(ctx context.Context, dev *infinitime.Device) error {
// This is where we'll mount the FS
err := os.MkdirAll(k.String("fuse.mountpoint"), 0755)
err := os.MkdirAll(k.String("fuse.mountpoint"), 0o755)
if err != nil && !os.IsExist(err) {
return err
}
@ -19,7 +21,6 @@ func startFUSE(ctx context.Context, dev *infinitime.Device) error {
// Ignore the error because nothing might be mounted on the mountpoint
_ = fusefs.Unmount(k.String("fuse.mountpoint"))
root, err := fusefs.BuildRootNode(dev)
if err != nil {
log.Error("Building root node failed").
@ -31,7 +32,7 @@ func startFUSE(ctx context.Context, dev *infinitime.Device) error {
server, err := fs.Mount(k.String("fuse.mountpoint"), root, &fs.Options{
MountOptions: fuse.MountOptions{
// Set to true to see how the file system works.
Debug: false,
Debug: false,
SingleThreaded: true,
},
})
@ -40,7 +41,7 @@ func startFUSE(ctx context.Context, dev *infinitime.Device) error {
Str("target", k.String("fuse.mountpoint")).
Err(err).
Send()
return err
return err
}
log.Info("Mounted on target").

View File

@ -1,44 +1,46 @@
package fusefs
import (
"bytes"
"context"
"io"
"strconv"
"syscall"
"github.com/hanwen/go-fuse/v2/fs"
"github.com/hanwen/go-fuse/v2/fuse"
"go.arsenm.dev/infinitime"
"go.arsenm.dev/infinitime/blefs"
"go.arsenm.dev/logger/log"
"context"
"syscall"
"github.com/hanwen/go-fuse/v2/fs"
"github.com/hanwen/go-fuse/v2/fuse"
"io"
"bytes"
"strconv"
)
type ITProperty struct {
name string
Ino uint64
gen func() ([]byte, error)
Ino uint64
gen func() ([]byte, error)
}
type DirEntry struct {
isDir bool
modtime uint64
size uint32
path string
isDir bool
modtime uint64
size uint32
path string
}
type ITNode struct {
fs.Inode
kind int
Ino uint64
Ino uint64
lst []DirEntry
lst []DirEntry
self DirEntry
path string
}
var myfs *blefs.FS = nil
var inodemap map[string]uint64 = nil
var (
myfs *blefs.FS = nil
inodemap map[string]uint64 = nil
)
func BuildRootNode(dev *infinitime.Device) (*ITNode, error) {
var err error
@ -55,40 +57,50 @@ func BuildRootNode(dev *infinitime.Device) (*ITNode, error) {
var properties = make([]ITProperty, 6)
func BuildProperties(dev *infinitime.Device) {
properties[0] = ITProperty{"heartrate", 2,
properties[0] = ITProperty{
"heartrate", 2,
func() ([]byte, error) {
ans, err := dev.HeartRate()
return []byte(strconv.Itoa(int(ans)) + "\n"), err
}}
properties[1] = ITProperty{"battery", 3,
},
}
properties[1] = ITProperty{
"battery", 3,
func() ([]byte, error) {
ans, err := dev.BatteryLevel()
return []byte(strconv.Itoa(int(ans)) + "\n"), err
}}
properties[2] = ITProperty{"motion", 4,
},
}
properties[2] = ITProperty{
"motion", 4,
func() ([]byte, error) {
ans, err := dev.Motion()
return []byte(strconv.Itoa(int(ans.X)) + " " + strconv.Itoa(int(ans.Y)) + " " + strconv.Itoa(int(ans.Z)) + "\n"), err
}}
properties[3] = ITProperty{"stepcount", 6,
},
}
properties[3] = ITProperty{
"stepcount", 6,
func() ([]byte, error) {
ans, err := dev.StepCount()
return []byte(strconv.Itoa(int(ans)) + "\n"), err
}}
properties[4] = ITProperty{"version", 7,
},
}
properties[4] = ITProperty{
"version", 7,
func() ([]byte, error) {
ans, err := dev.Version()
return []byte(ans + "\n"), err
}}
properties[5] = ITProperty{"address", 8,
},
}
properties[5] = ITProperty{
"address", 8,
func() ([]byte, error) {
ans := dev.Address()
return []byte(ans + "\n"), nil
}}
},
}
}
var _ fs.NodeReaddirer = (*ITNode)(nil)
// Readdir is part of the NodeReaddirer interface
@ -142,10 +154,10 @@ func (n *ITNode) Readdir(ctx context.Context) (fs.DirStream, syscall.Errno) {
name := info.Name()
file := DirEntry{
path: n.path + "/" + name,
size: uint32(info.Size()),
path: n.path + "/" + name,
size: uint32(info.Size()),
modtime: uint64(info.ModTime().Unix()),
isDir: info.IsDir(),
isDir: info.IsDir(),
}
n.lst[ind] = file
@ -159,13 +171,13 @@ func (n *ITNode) Readdir(ctx context.Context) (fs.DirStream, syscall.Errno) {
r[ind] = fuse.DirEntry{
Name: name,
Mode: fuse.S_IFDIR,
Ino : ino + 10,
Ino: ino + 10,
}
} else {
r[ind] = fuse.DirEntry{
Name: name,
Mode: fuse.S_IFREG,
Ino : ino + 10,
Ino: ino + 10,
}
}
}
@ -176,6 +188,7 @@ func (n *ITNode) Readdir(ctx context.Context) (fs.DirStream, syscall.Errno) {
}
var _ fs.NodeLookuper = (*ITNode)(nil)
func (n *ITNode) Lookup(ctx context.Context, name string, out *fuse.EntryOut) (*fs.Inode, syscall.Errno) {
switch n.kind {
case 0:
@ -183,7 +196,7 @@ func (n *ITNode) Lookup(ctx context.Context, name string, out *fuse.EntryOut) (*
if name == "info" {
stable := fs.StableAttr{
Mode: fuse.S_IFDIR,
Ino: uint64(0),
Ino: uint64(0),
}
operations := &ITNode{kind: 1, Ino: 0}
child := n.NewInode(ctx, operations, stable)
@ -191,9 +204,9 @@ func (n *ITNode) Lookup(ctx context.Context, name string, out *fuse.EntryOut) (*
} else if name == "fs" {
stable := fs.StableAttr{
Mode: fuse.S_IFDIR,
Ino: uint64(1),
Ino: uint64(1),
}
operations := &ITNode{kind: 2, Ino: 1, path : ""}
operations := &ITNode{kind: 2, Ino: 1, path: ""}
child := n.NewInode(ctx, operations, stable)
return child, 0
}
@ -203,7 +216,7 @@ func (n *ITNode) Lookup(ctx context.Context, name string, out *fuse.EntryOut) (*
if value.name == name {
stable := fs.StableAttr{
Mode: fuse.S_IFREG,
Ino: uint64(value.Ino),
Ino: uint64(value.Ino),
}
operations := &ITNode{kind: 3, Ino: value.Ino}
child := n.NewInode(ctx, operations, stable)
@ -218,7 +231,7 @@ func (n *ITNode) Lookup(ctx context.Context, name string, out *fuse.EntryOut) (*
}
for _, file := range n.lst {
if file.path != n.path + "/" + name {
if file.path != n.path+"/"+name {
continue
}
log.Debug("FUSE Lookup successful").Str("path", file.path).Send()
@ -226,7 +239,7 @@ func (n *ITNode) Lookup(ctx context.Context, name string, out *fuse.EntryOut) (*
if file.isDir {
stable := fs.StableAttr{
Mode: fuse.S_IFDIR,
Ino: inodemap[file.path],
Ino: inodemap[file.path],
}
operations := &ITNode{kind: 2, path: file.path}
child := n.NewInode(ctx, operations, stable)
@ -234,7 +247,7 @@ func (n *ITNode) Lookup(ctx context.Context, name string, out *fuse.EntryOut) (*
} else {
stable := fs.StableAttr{
Mode: fuse.S_IFREG,
Ino: inodemap[file.path],
Ino: inodemap[file.path],
}
operations := &ITNode{
kind: 2, path: file.path,
@ -245,7 +258,7 @@ func (n *ITNode) Lookup(ctx context.Context, name string, out *fuse.EntryOut) (*
}
break
}
log.Warn("FUSE Lookup failed").Str("path", n.path + "/" + name).Send()
log.Warn("FUSE Lookup failed").Str("path", n.path+"/"+name).Send()
}
return nil, syscall.ENOENT
}
@ -255,6 +268,7 @@ type bytesFileReadHandle struct {
}
var _ fs.FileReader = (*bytesFileReadHandle)(nil)
func (fh *bytesFileReadHandle) Read(ctx context.Context, dest []byte, off int64) (fuse.ReadResult, syscall.Errno) {
log.Debug("FUSE Executing Read").Int("size", len(fh.content)).Send()
end := off + int64(len(dest))
@ -267,7 +281,9 @@ func (fh *bytesFileReadHandle) Read(ctx context.Context, dest []byte, off int64)
type sensorFileReadHandle struct {
content []byte
}
var _ fs.FileReader = (*sensorFileReadHandle)(nil)
func (fh *sensorFileReadHandle) Read(ctx context.Context, dest []byte, off int64) (fuse.ReadResult, syscall.Errno) {
log.Info("Executing Read").Int("size", len(fh.content)).Send()
end := off + int64(len(dest))
@ -278,17 +294,18 @@ func (fh *sensorFileReadHandle) Read(ctx context.Context, dest []byte, off int64
}
var _ fs.FileFlusher = (*sensorFileReadHandle)(nil)
func (fh *sensorFileReadHandle) Flush(ctx context.Context) (errno syscall.Errno) {
return 0
}
type bytesFileWriteHandle struct {
content []byte
path string
path string
}
var _ fs.FileWriter = (*bytesFileWriteHandle)(nil)
func (fh *bytesFileWriteHandle) Write(ctx context.Context, data []byte, off int64) (written uint32, errno syscall.Errno) {
log.Info("Executing Write").Str("path", fh.path).Int("prev_size", len(fh.content)).Int("next_size", len(data)).Send()
if off != int64(len(fh.content)) {
@ -300,8 +317,8 @@ func (fh *bytesFileWriteHandle) Write(ctx context.Context, data []byte, off int6
}
var _ fs.FileFlusher = (*bytesFileWriteHandle)(nil)
func (fh *bytesFileWriteHandle) Flush(ctx context.Context) (errno syscall.Errno) {
func (fh *bytesFileWriteHandle) Flush(ctx context.Context) (errno syscall.Errno) {
log.Debug("FUSE Attempting flush").Str("path", fh.path).Send()
fp, err := myfs.Create(fh.path, uint32(len(fh.content)))
if err != nil {
@ -347,12 +364,15 @@ func (fh *bytesFileWriteHandle) Flush(ctx context.Context) (errno syscall.Errno)
return 0
}
var _ fs.FileFsyncer = (*bytesFileWriteHandle)(nil)
func (fh *bytesFileWriteHandle) Fsync(ctx context.Context, flags uint32) (errno syscall.Errno) {
return fh.Flush(ctx)
}
var _ fs.NodeGetattrer = (*ITNode)(nil)
func (bn *ITNode) Getattr(ctx context.Context, f fs.FileHandle, out *fuse.AttrOut) syscall.Errno {
log.Debug("FUSE getattr").Str("path", bn.path).Send()
out.Ino = bn.Ino
@ -364,6 +384,7 @@ func (bn *ITNode) Getattr(ctx context.Context, f fs.FileHandle, out *fuse.AttrOu
}
var _ fs.NodeSetattrer = (*ITNode)(nil)
func (bn *ITNode) Setattr(ctx context.Context, fh fs.FileHandle, in *fuse.SetAttrIn, out *fuse.AttrOut) syscall.Errno {
log.Debug("FUSE setattr").Str("path", bn.path).Send()
out.Size = 0
@ -372,6 +393,7 @@ func (bn *ITNode) Setattr(ctx context.Context, fh fs.FileHandle, in *fuse.SetAtt
}
var _ fs.NodeOpener = (*ITNode)(nil)
func (f *ITNode) Open(ctx context.Context, openFlags uint32) (fh fs.FileHandle, fuseFlags uint32, errno syscall.Errno) {
switch f.kind {
case 2:
@ -381,11 +403,11 @@ func (f *ITNode) Open(ctx context.Context, openFlags uint32) (fh fs.FileHandle,
return nil, 0, syscall.EROFS
}
if openFlags & syscall.O_WRONLY != 0 {
if openFlags&syscall.O_WRONLY != 0 {
log.Debug("FUSE Opening for write").Str("path", f.path).Send()
fh = &bytesFileWriteHandle{
path : f.path,
content : make([]byte, 0),
path: f.path,
content: make([]byte, 0),
}
return fh, fuse.FOPEN_DIRECT_IO, 0
} else {
@ -436,7 +458,7 @@ func (f *ITNode) Open(ctx context.Context, openFlags uint32) (fh fs.FileHandle,
}
fh = &sensorFileReadHandle{
content : ans,
content: ans,
}
return fh, fuse.FOPEN_DIRECT_IO, 0
}
@ -446,6 +468,7 @@ func (f *ITNode) Open(ctx context.Context, openFlags uint32) (fh fs.FileHandle,
}
var _ fs.NodeCreater = (*ITNode)(nil)
func (f *ITNode) Create(ctx context.Context, name string, flags uint32, mode uint32, out *fuse.EntryOut) (node *fs.Inode, fh fs.FileHandle, fuseFlags uint32, errno syscall.Errno) {
if f.kind != 2 {
return nil, nil, 0, syscall.EROFS
@ -457,17 +480,17 @@ func (f *ITNode) Create(ctx context.Context, name string, flags uint32, mode uin
stable := fs.StableAttr{
Mode: fuse.S_IFREG,
Ino: ino,
Ino: ino,
}
operations := &ITNode{
kind: 2, Ino: ino,
path : path,
path: path,
}
node = f.NewInode(ctx, operations, stable)
fh = &bytesFileWriteHandle{
path : path,
content : make([]byte, 0),
path: path,
content: make([]byte, 0),
}
log.Debug("FUSE Creating file").Str("path", path).Send()
@ -477,6 +500,7 @@ func (f *ITNode) Create(ctx context.Context, name string, flags uint32, mode uin
}
var _ fs.NodeMkdirer = (*ITNode)(nil)
func (f *ITNode) Mkdir(ctx context.Context, name string, mode uint32, out *fuse.EntryOut) (*fs.Inode, syscall.Errno) {
if f.kind != 2 {
return nil, syscall.EROFS
@ -497,11 +521,11 @@ func (f *ITNode) Mkdir(ctx context.Context, name string, mode uint32, out *fuse.
stable := fs.StableAttr{
Mode: fuse.S_IFDIR,
Ino: ino,
Ino: ino,
}
operations := &ITNode{
kind: 2, Ino: ino,
path : path,
path: path,
}
node := f.NewInode(ctx, operations, stable)
@ -513,6 +537,7 @@ func (f *ITNode) Mkdir(ctx context.Context, name string, mode uint32, out *fuse.
}
var _ fs.NodeRenamer = (*ITNode)(nil)
func (f *ITNode) Rename(ctx context.Context, name string, newParent fs.InodeEmbedder, newName string, flags uint32) syscall.Errno {
if f.kind != 2 {
return syscall.EROFS
@ -544,16 +569,17 @@ func (f *ITNode) Rename(ctx context.Context, name string, newParent fs.InodeEmbe
}
var _ fs.NodeUnlinker = (*ITNode)(nil)
func (f *ITNode) Unlink(ctx context.Context, name string) syscall.Errno {
if f.kind != 2 {
return syscall.EROFS
}
delete(inodemap, f.path + "/" + name)
delete(inodemap, f.path+"/"+name)
err := myfs.Remove(f.path + "/" + name)
if err != nil {
log.Error("FUSE Unlink failed").
Str("file", f.path + "/" + name).
Str("file", f.path+"/"+name).
Err(err).
Send()
@ -561,12 +587,13 @@ func (f *ITNode) Unlink(ctx context.Context, name string) syscall.Errno {
}
log.Debug("FUSE Unlink success").
Str("file", f.path + "/" + name).
Str("file", f.path+"/"+name).
Send()
return 0
}
var _ fs.NodeRmdirer = (*ITNode)(nil)
func (f *ITNode) Rmdir(ctx context.Context, name string) syscall.Errno {
return f.Unlink(ctx, name)
}

View File

@ -1,7 +1,9 @@
package fusefs
import (
"go.arsenm.dev/infinitime/blefs"
"syscall"
"go.arsenm.dev/infinitime/blefs"
)
func syscallErr(err error) syscall.Errno {
@ -51,7 +53,7 @@ func syscallErr(err error) syscall.Errno {
case blefs.ErrFileWriteOnly: // file is write only
return syscall.EACCES
case blefs.ErrInvalidOffset: // invalid file offset
return syscall.EFAULT // TODO
return syscall.EFAULT // TODO
case blefs.ErrOffsetChanged: // offset has already been changed
return syscall.ESPIPE
case blefs.ErrReadOpen: // only one file can be opened for reading at a time
@ -63,5 +65,4 @@ func syscallErr(err error) syscall.Errno {
}
return syscall.EIO // TODO
}

View File

@ -2,6 +2,7 @@ package fusefs
import (
_ "unsafe"
"github.com/hanwen/go-fuse/v2/fuse"
)
@ -11,5 +12,6 @@ func Unmount(mountPoint string) error {
// Unfortunately, the FUSE library does not export its unmount function,
// so this is required until that changes
//
//go:linkname unmount github.com/hanwen/go-fuse/v2/fuse.unmount
func unmount(mountPoint string, opts *fuse.MountOptions) error