Run formatter

This commit is contained in:
Elara 2023-03-25 15:24:46 -07:00
parent 77680704e1
commit 510f183db2
5 changed files with 103 additions and 72 deletions

11
fuse.go
View File

@ -1,17 +1,19 @@
package main package main
import ( import (
"go.arsenm.dev/itd/internal/fusefs" "context"
"os" "os"
"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"
"go.arsenm.dev/logger/log"
"context"
"go.arsenm.dev/infinitime" "go.arsenm.dev/infinitime"
"go.arsenm.dev/itd/internal/fusefs"
"go.arsenm.dev/logger/log"
) )
func startFUSE(ctx context.Context, dev *infinitime.Device) error { func startFUSE(ctx context.Context, dev *infinitime.Device) error {
// This is where we'll mount the FS // 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) { if err != nil && !os.IsExist(err) {
return 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 // Ignore the error because nothing might be mounted on the mountpoint
_ = fusefs.Unmount(k.String("fuse.mountpoint")) _ = fusefs.Unmount(k.String("fuse.mountpoint"))
root, err := fusefs.BuildRootNode(dev) root, err := fusefs.BuildRootNode(dev)
if err != nil { if err != nil {
log.Error("Building root node failed"). log.Error("Building root node failed").

View File

@ -1,16 +1,17 @@
package fusefs package fusefs
import ( 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"
"go.arsenm.dev/infinitime/blefs" "go.arsenm.dev/infinitime/blefs"
"go.arsenm.dev/logger/log" "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 { type ITProperty struct {
@ -19,7 +20,6 @@ type ITProperty struct {
gen func() ([]byte, error) gen func() ([]byte, error)
} }
type DirEntry struct { type DirEntry struct {
isDir bool isDir bool
modtime uint64 modtime uint64
@ -37,8 +37,10 @@ type ITNode struct {
path string path string
} }
var myfs *blefs.FS = nil var (
var inodemap map[string]uint64 = nil myfs *blefs.FS = nil
inodemap map[string]uint64 = nil
)
func BuildRootNode(dev *infinitime.Device) (*ITNode, error) { func BuildRootNode(dev *infinitime.Device) (*ITNode, error) {
var err error var err error
@ -55,39 +57,49 @@ func BuildRootNode(dev *infinitime.Device) (*ITNode, error) {
var properties = make([]ITProperty, 6) var properties = make([]ITProperty, 6)
func BuildProperties(dev *infinitime.Device) { func BuildProperties(dev *infinitime.Device) {
properties[0] = ITProperty{"heartrate", 2, properties[0] = ITProperty{
"heartrate", 2,
func() ([]byte, error) { func() ([]byte, error) {
ans, err := dev.HeartRate() ans, err := dev.HeartRate()
return []byte(strconv.Itoa(int(ans)) + "\n"), err return []byte(strconv.Itoa(int(ans)) + "\n"), err
}} },
properties[1] = ITProperty{"battery", 3, }
properties[1] = ITProperty{
"battery", 3,
func() ([]byte, error) { func() ([]byte, error) {
ans, err := dev.BatteryLevel() ans, err := dev.BatteryLevel()
return []byte(strconv.Itoa(int(ans)) + "\n"), err return []byte(strconv.Itoa(int(ans)) + "\n"), err
}} },
properties[2] = ITProperty{"motion", 4, }
properties[2] = ITProperty{
"motion", 4,
func() ([]byte, error) { func() ([]byte, error) {
ans, err := dev.Motion() ans, err := dev.Motion()
return []byte(strconv.Itoa(int(ans.X)) + " " + strconv.Itoa(int(ans.Y)) + " " + strconv.Itoa(int(ans.Z)) + "\n"), err 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) { func() ([]byte, error) {
ans, err := dev.StepCount() ans, err := dev.StepCount()
return []byte(strconv.Itoa(int(ans)) + "\n"), err return []byte(strconv.Itoa(int(ans)) + "\n"), err
}} },
properties[4] = ITProperty{"version", 7, }
properties[4] = ITProperty{
"version", 7,
func() ([]byte, error) { func() ([]byte, error) {
ans, err := dev.Version() ans, err := dev.Version()
return []byte(ans + "\n"), err return []byte(ans + "\n"), err
}} },
properties[5] = ITProperty{"address", 8, }
properties[5] = ITProperty{
"address", 8,
func() ([]byte, error) { func() ([]byte, error) {
ans := dev.Address() ans := dev.Address()
return []byte(ans + "\n"), nil return []byte(ans + "\n"), nil
}} },
}
} }
var _ fs.NodeReaddirer = (*ITNode)(nil) var _ fs.NodeReaddirer = (*ITNode)(nil)
@ -176,6 +188,7 @@ func (n *ITNode) Readdir(ctx context.Context) (fs.DirStream, syscall.Errno) {
} }
var _ fs.NodeLookuper = (*ITNode)(nil) var _ fs.NodeLookuper = (*ITNode)(nil)
func (n *ITNode) Lookup(ctx context.Context, name string, out *fuse.EntryOut) (*fs.Inode, syscall.Errno) { func (n *ITNode) Lookup(ctx context.Context, name string, out *fuse.EntryOut) (*fs.Inode, syscall.Errno) {
switch n.kind { switch n.kind {
case 0: case 0:
@ -255,6 +268,7 @@ type bytesFileReadHandle struct {
} }
var _ fs.FileReader = (*bytesFileReadHandle)(nil) var _ fs.FileReader = (*bytesFileReadHandle)(nil)
func (fh *bytesFileReadHandle) Read(ctx context.Context, dest []byte, off int64) (fuse.ReadResult, syscall.Errno) { 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() log.Debug("FUSE Executing Read").Int("size", len(fh.content)).Send()
end := off + int64(len(dest)) end := off + int64(len(dest))
@ -267,7 +281,9 @@ func (fh *bytesFileReadHandle) Read(ctx context.Context, dest []byte, off int64)
type sensorFileReadHandle struct { type sensorFileReadHandle struct {
content []byte content []byte
} }
var _ fs.FileReader = (*sensorFileReadHandle)(nil) var _ fs.FileReader = (*sensorFileReadHandle)(nil)
func (fh *sensorFileReadHandle) Read(ctx context.Context, dest []byte, off int64) (fuse.ReadResult, syscall.Errno) { 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() log.Info("Executing Read").Int("size", len(fh.content)).Send()
end := off + int64(len(dest)) 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) var _ fs.FileFlusher = (*sensorFileReadHandle)(nil)
func (fh *sensorFileReadHandle) Flush(ctx context.Context) (errno syscall.Errno) { func (fh *sensorFileReadHandle) Flush(ctx context.Context) (errno syscall.Errno) {
return 0 return 0
} }
type bytesFileWriteHandle struct { type bytesFileWriteHandle struct {
content []byte content []byte
path string path string
} }
var _ fs.FileWriter = (*bytesFileWriteHandle)(nil) var _ fs.FileWriter = (*bytesFileWriteHandle)(nil)
func (fh *bytesFileWriteHandle) Write(ctx context.Context, data []byte, off int64) (written uint32, errno syscall.Errno) { 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() 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)) { 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) 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() log.Debug("FUSE Attempting flush").Str("path", fh.path).Send()
fp, err := myfs.Create(fh.path, uint32(len(fh.content))) fp, err := myfs.Create(fh.path, uint32(len(fh.content)))
if err != nil { if err != nil {
@ -347,12 +364,15 @@ func (fh *bytesFileWriteHandle) Flush(ctx context.Context) (errno syscall.Errno)
return 0 return 0
} }
var _ fs.FileFsyncer = (*bytesFileWriteHandle)(nil) var _ fs.FileFsyncer = (*bytesFileWriteHandle)(nil)
func (fh *bytesFileWriteHandle) Fsync(ctx context.Context, flags uint32) (errno syscall.Errno) { func (fh *bytesFileWriteHandle) Fsync(ctx context.Context, flags uint32) (errno syscall.Errno) {
return fh.Flush(ctx) return fh.Flush(ctx)
} }
var _ fs.NodeGetattrer = (*ITNode)(nil) var _ fs.NodeGetattrer = (*ITNode)(nil)
func (bn *ITNode) Getattr(ctx context.Context, f fs.FileHandle, out *fuse.AttrOut) syscall.Errno { func (bn *ITNode) Getattr(ctx context.Context, f fs.FileHandle, out *fuse.AttrOut) syscall.Errno {
log.Debug("FUSE getattr").Str("path", bn.path).Send() log.Debug("FUSE getattr").Str("path", bn.path).Send()
out.Ino = bn.Ino 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) var _ fs.NodeSetattrer = (*ITNode)(nil)
func (bn *ITNode) Setattr(ctx context.Context, fh fs.FileHandle, in *fuse.SetAttrIn, out *fuse.AttrOut) syscall.Errno { 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() log.Debug("FUSE setattr").Str("path", bn.path).Send()
out.Size = 0 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) var _ fs.NodeOpener = (*ITNode)(nil)
func (f *ITNode) Open(ctx context.Context, openFlags uint32) (fh fs.FileHandle, fuseFlags uint32, errno syscall.Errno) { func (f *ITNode) Open(ctx context.Context, openFlags uint32) (fh fs.FileHandle, fuseFlags uint32, errno syscall.Errno) {
switch f.kind { switch f.kind {
case 2: case 2:
@ -446,6 +468,7 @@ func (f *ITNode) Open(ctx context.Context, openFlags uint32) (fh fs.FileHandle,
} }
var _ fs.NodeCreater = (*ITNode)(nil) 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) { 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 { if f.kind != 2 {
return nil, nil, 0, syscall.EROFS return nil, nil, 0, syscall.EROFS
@ -477,6 +500,7 @@ func (f *ITNode) Create(ctx context.Context, name string, flags uint32, mode uin
} }
var _ fs.NodeMkdirer = (*ITNode)(nil) var _ fs.NodeMkdirer = (*ITNode)(nil)
func (f *ITNode) Mkdir(ctx context.Context, name string, mode uint32, out *fuse.EntryOut) (*fs.Inode, syscall.Errno) { func (f *ITNode) Mkdir(ctx context.Context, name string, mode uint32, out *fuse.EntryOut) (*fs.Inode, syscall.Errno) {
if f.kind != 2 { if f.kind != 2 {
return nil, syscall.EROFS return nil, syscall.EROFS
@ -513,6 +537,7 @@ func (f *ITNode) Mkdir(ctx context.Context, name string, mode uint32, out *fuse.
} }
var _ fs.NodeRenamer = (*ITNode)(nil) var _ fs.NodeRenamer = (*ITNode)(nil)
func (f *ITNode) Rename(ctx context.Context, name string, newParent fs.InodeEmbedder, newName string, flags uint32) syscall.Errno { func (f *ITNode) Rename(ctx context.Context, name string, newParent fs.InodeEmbedder, newName string, flags uint32) syscall.Errno {
if f.kind != 2 { if f.kind != 2 {
return syscall.EROFS return syscall.EROFS
@ -544,6 +569,7 @@ func (f *ITNode) Rename(ctx context.Context, name string, newParent fs.InodeEmbe
} }
var _ fs.NodeUnlinker = (*ITNode)(nil) var _ fs.NodeUnlinker = (*ITNode)(nil)
func (f *ITNode) Unlink(ctx context.Context, name string) syscall.Errno { func (f *ITNode) Unlink(ctx context.Context, name string) syscall.Errno {
if f.kind != 2 { if f.kind != 2 {
return syscall.EROFS return syscall.EROFS
@ -567,6 +593,7 @@ func (f *ITNode) Unlink(ctx context.Context, name string) syscall.Errno {
} }
var _ fs.NodeRmdirer = (*ITNode)(nil) var _ fs.NodeRmdirer = (*ITNode)(nil)
func (f *ITNode) Rmdir(ctx context.Context, name string) syscall.Errno { func (f *ITNode) Rmdir(ctx context.Context, name string) syscall.Errno {
return f.Unlink(ctx, name) return f.Unlink(ctx, name)
} }

View File

@ -1,7 +1,9 @@
package fusefs package fusefs
import ( import (
"go.arsenm.dev/infinitime/blefs"
"syscall" "syscall"
"go.arsenm.dev/infinitime/blefs"
) )
func syscallErr(err error) syscall.Errno { func syscallErr(err error) syscall.Errno {
@ -63,5 +65,4 @@ func syscallErr(err error) syscall.Errno {
} }
return syscall.EIO // TODO return syscall.EIO // TODO
} }

View File

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