forked from Elara6331/itd
		
	Compare commits
	
		
			17 Commits
		
	
	
		
			c05d4fe951
			...
			fuse
		
	
	| Author | SHA1 | Date | |
|---|---|---|---|
| 
						 | 
					008f6b35a9 | ||
| 
						 | 
					98775600af | ||
| 
						 | 
					613d33ff4d | ||
| 
						 | 
					1a1bc30df9 | ||
| 
						 | 
					c5a6e0d298 | ||
| 
						 | 
					84c7a33c40 | ||
| 
						 | 
					dc53ead339 | ||
| 
						 | 
					9bbdc3bd52 | ||
| 
						 | 
					b28c386c4e | ||
| 
						 | 
					4c59561a99 | ||
| 
						 | 
					955e1323ce | ||
| 
						 | 
					3b9690103b | ||
| 
						 | 
					b5328ece92 | ||
| 
						 | 
					2396623c73 | ||
| 
						 | 
					a54ca7afdf | ||
| 
						 | 
					673383f795 | ||
| 
						 | 
					cc6fc3e1dc | 
							
								
								
									
										21
									
								
								fuse.go
									
									
									
									
									
								
							
							
						
						
									
										21
									
								
								fuse.go
									
									
									
									
									
								
							@@ -9,10 +9,25 @@ import (
 | 
			
		||||
	"go.arsenm.dev/infinitime"
 | 
			
		||||
)
 | 
			
		||||
 | 
			
		||||
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
 | 
			
		||||
	os.Mkdir(k.String("fuse.mountpoint"), 0755)
 | 
			
		||||
	root := fusefs.BuildRootNode(dev)
 | 
			
		||||
	err := os.MkdirAll(k.String("fuse.mountpoint"), 0755)
 | 
			
		||||
	if err != nil && !os.IsExist(err) {
 | 
			
		||||
		return err
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	// 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").
 | 
			
		||||
			Err(err).
 | 
			
		||||
			Send()
 | 
			
		||||
		return err
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	server, err := fs.Mount(k.String("fuse.mountpoint"), root, &fs.Options{
 | 
			
		||||
		MountOptions: fuse.MountOptions{
 | 
			
		||||
			// Set to true to see how the file system works.
 | 
			
		||||
 
 | 
			
		||||
							
								
								
									
										1
									
								
								go.sum
									
									
									
									
									
								
							
							
						
						
									
										1
									
								
								go.sum
									
									
									
									
									
								
							@@ -341,7 +341,6 @@ github.com/kr/pretty v0.2.0/go.mod h1:ipq/a2n7PKx3OHsz4KJII5eveXtPO4qwEXGdVfWzfn
 | 
			
		||||
github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
 | 
			
		||||
github.com/kr/text v0.1.0 h1:45sCR5RtlFHMR4UwH9sdQ5TC8v0qDQCHnXt+kaKSTVE=
 | 
			
		||||
github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
 | 
			
		||||
github.com/kylelemons/godebug v0.0.0-20170820004349-d65d576e9348/go.mod h1:B69LEHPfb2qLo0BaaOLcbitczOKLWTsrBG9LczfCD4k=
 | 
			
		||||
github.com/lucor/goinfo v0.0.0-20210802170112-c078a2b0f08b/go.mod h1:PRq09yoB+Q2OJReAmwzKivcYyremnibWGbK7WfftHzc=
 | 
			
		||||
github.com/magiconair/properties v1.8.5/go.mod h1:y3VJvCyxH9uVvJTWEGAELF3aiYNyPKd5NZ3oSwXrF60=
 | 
			
		||||
github.com/mattn/go-colorable v0.0.9/go.mod h1:9vuHe8Xs5qXnSaW/c/ABM9alt+Vo+STaOChaDxuIBZU=
 | 
			
		||||
 
 | 
			
		||||
@@ -1,61 +0,0 @@
 | 
			
		||||
package fusefs
 | 
			
		||||
 | 
			
		||||
import (
 | 
			
		||||
	"go.arsenm.dev/infinitime"
 | 
			
		||||
	"context"
 | 
			
		||||
	"strconv"
 | 
			
		||||
)
 | 
			
		||||
 | 
			
		||||
func converterU8(ctx context.Context, in <-chan uint8) <-chan []byte {
 | 
			
		||||
	out := make(chan []byte, 2)
 | 
			
		||||
	go func() {
 | 
			
		||||
		for {
 | 
			
		||||
			select {
 | 
			
		||||
			case <- ctx.Done():
 | 
			
		||||
				return
 | 
			
		||||
			case event := <-in:
 | 
			
		||||
				out <- []byte(strconv.Itoa(int(event)) + "\n")
 | 
			
		||||
			}
 | 
			
		||||
		}
 | 
			
		||||
	}()
 | 
			
		||||
	return out
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func converterU32(ctx context.Context, in <-chan uint32) <-chan []byte {
 | 
			
		||||
	out := make(chan []byte, 2)
 | 
			
		||||
	go func() {
 | 
			
		||||
		for {
 | 
			
		||||
			select {
 | 
			
		||||
			case <- ctx.Done():
 | 
			
		||||
				return
 | 
			
		||||
			case event := <-in:
 | 
			
		||||
				out <- []byte(strconv.Itoa(int(event)) + "\n")
 | 
			
		||||
			}
 | 
			
		||||
		}
 | 
			
		||||
	}()
 | 
			
		||||
	return out
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func converterMotionValues(ctx context.Context, in <-chan infinitime.MotionValues) <-chan []byte {
 | 
			
		||||
	out := make(chan []byte, 2)
 | 
			
		||||
	go func() {
 | 
			
		||||
		for {
 | 
			
		||||
			select {
 | 
			
		||||
			case <- ctx.Done():
 | 
			
		||||
				return
 | 
			
		||||
			case event := <-in:
 | 
			
		||||
				out <- []byte(strconv.Itoa(int(event.X)) + " " + strconv.Itoa(int(event.Y)) + " " + strconv.Itoa(int(event.Z)) + "\n")
 | 
			
		||||
			}
 | 
			
		||||
		}
 | 
			
		||||
	}()
 | 
			
		||||
	return out
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func converter1String(ctx context.Context, in string) <-chan []byte {
 | 
			
		||||
	out := make(chan []byte, 2)
 | 
			
		||||
	out <- []byte(in + "\n")
 | 
			
		||||
	close(out)
 | 
			
		||||
	return out
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
@@ -10,12 +10,13 @@ import (
 | 
			
		||||
	"github.com/hanwen/go-fuse/v2/fuse"
 | 
			
		||||
	"io"
 | 
			
		||||
	"bytes"
 | 
			
		||||
	"strconv"
 | 
			
		||||
)
 | 
			
		||||
 | 
			
		||||
type ITProperty struct {
 | 
			
		||||
	name string
 | 
			
		||||
	Ino uint64
 | 
			
		||||
	gen func(context.Context) (<-chan []byte, error)
 | 
			
		||||
	gen func() ([]byte, error)
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
@@ -36,55 +37,59 @@ type ITNode struct {
 | 
			
		||||
	path string
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func BuildRootNode(dev *infinitime.Device) *ITNode {
 | 
			
		||||
	inodemap = make(map[string]uint64)
 | 
			
		||||
	myfs, _ = dev.FS()
 | 
			
		||||
var myfs *blefs.FS = nil
 | 
			
		||||
var inodemap map[string]uint64 = nil
 | 
			
		||||
 | 
			
		||||
	return &ITNode{kind: 0}
 | 
			
		||||
func BuildRootNode(dev *infinitime.Device) (*ITNode, error) {
 | 
			
		||||
	var err error
 | 
			
		||||
	inodemap = make(map[string]uint64)
 | 
			
		||||
	myfs, err = dev.FS()
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		log.Error("FUSE Failed to get filesystem").Err(err).Send()
 | 
			
		||||
		return nil, err
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	return &ITNode{kind: 0}, nil
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
var properties = make([]ITProperty, 6)
 | 
			
		||||
 | 
			
		||||
func BuildProperties(dev *infinitime.Device) {
 | 
			
		||||
	properties[0] = ITProperty{"heartrate", 2,
 | 
			
		||||
		func(ctx context.Context) (<-chan []byte, error) {
 | 
			
		||||
			ans, err := dev.WatchHeartRate(ctx)
 | 
			
		||||
			return converterU8(ctx, ans), err
 | 
			
		||||
		func() ([]byte, error) {
 | 
			
		||||
			ans, err := dev.HeartRate()
 | 
			
		||||
			return []byte(strconv.Itoa(int(ans)) + "\n"), err
 | 
			
		||||
	}}
 | 
			
		||||
	properties[1] = ITProperty{"battery", 3,
 | 
			
		||||
		func(ctx context.Context) (<-chan []byte, error) {
 | 
			
		||||
			ans, err := dev.WatchBatteryLevel(ctx)
 | 
			
		||||
			return converterU8(ctx, ans), err
 | 
			
		||||
		func() ([]byte, error) {
 | 
			
		||||
			ans, err := dev.BatteryLevel()
 | 
			
		||||
			return []byte(strconv.Itoa(int(ans)) + "\n"), err
 | 
			
		||||
	}}
 | 
			
		||||
	properties[2] = ITProperty{"motion", 4,
 | 
			
		||||
		func(ctx context.Context) (<-chan []byte, error) {
 | 
			
		||||
			ans, err := dev.WatchMotion(ctx)
 | 
			
		||||
			return converterMotionValues(ctx, ans), err
 | 
			
		||||
		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", 5,
 | 
			
		||||
		func(ctx context.Context) (<-chan []byte, error) {
 | 
			
		||||
			ans, err := dev.WatchStepCount(ctx)
 | 
			
		||||
			return converterU32(ctx, ans), err
 | 
			
		||||
	properties[3] = ITProperty{"stepcount", 6,
 | 
			
		||||
		func() ([]byte, error) {
 | 
			
		||||
			ans, err := dev.StepCount()
 | 
			
		||||
			return []byte(strconv.Itoa(int(ans)) + "\n"), err
 | 
			
		||||
	}}
 | 
			
		||||
	properties[4] = ITProperty{"version", 6,
 | 
			
		||||
		func(ctx context.Context) (<-chan []byte, error) {
 | 
			
		||||
	properties[4] = ITProperty{"version", 7,
 | 
			
		||||
		func() ([]byte, error) {
 | 
			
		||||
			ans, err := dev.Version()
 | 
			
		||||
			return converter1String(ctx, ans), err
 | 
			
		||||
			return []byte(ans + "\n"), err
 | 
			
		||||
	}}
 | 
			
		||||
	properties[5] = ITProperty{"address", 7,
 | 
			
		||||
		func(ctx context.Context) (<-chan []byte, error) {
 | 
			
		||||
	properties[5] = ITProperty{"address", 8,
 | 
			
		||||
		func() ([]byte, error) {
 | 
			
		||||
			ans := dev.Address()
 | 
			
		||||
			return converter1String(ctx, ans), nil
 | 
			
		||||
			return []byte(ans + "\n"), nil
 | 
			
		||||
	}}
 | 
			
		||||
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
var myfs *blefs.FS = nil;
 | 
			
		||||
var inodemap map[string]uint64 = nil;
 | 
			
		||||
 | 
			
		||||
var _ = (fs.NodeReaddirer)((*ITNode)(nil))
 | 
			
		||||
var _ fs.NodeReaddirer = (*ITNode)(nil)
 | 
			
		||||
 | 
			
		||||
// Readdir is part of the NodeReaddirer interface
 | 
			
		||||
func (n *ITNode) Readdir(ctx context.Context) (fs.DirStream, syscall.Errno) {
 | 
			
		||||
@@ -93,7 +98,7 @@ func (n *ITNode) Readdir(ctx context.Context) (fs.DirStream, syscall.Errno) {
 | 
			
		||||
		// root folder
 | 
			
		||||
		r := make([]fuse.DirEntry, 2)
 | 
			
		||||
		r[0] = fuse.DirEntry{
 | 
			
		||||
			Name: "device",
 | 
			
		||||
			Name: "info",
 | 
			
		||||
			Ino:  0,
 | 
			
		||||
			Mode: fuse.S_IFDIR,
 | 
			
		||||
		}
 | 
			
		||||
@@ -105,7 +110,7 @@ func (n *ITNode) Readdir(ctx context.Context) (fs.DirStream, syscall.Errno) {
 | 
			
		||||
		return fs.NewListDirStream(r), 0
 | 
			
		||||
 | 
			
		||||
	case 1:
 | 
			
		||||
		// device folder
 | 
			
		||||
		// info folder
 | 
			
		||||
		r := make([]fuse.DirEntry, 6)
 | 
			
		||||
		for ind, value := range properties {
 | 
			
		||||
			r[ind] = fuse.DirEntry{
 | 
			
		||||
@@ -118,13 +123,22 @@ 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)
 | 
			
		||||
		log.Info("readdir").Str("path", n.path).Int("objects", len(files)).Send()
 | 
			
		||||
		// on info
 | 
			
		||||
		files, err := myfs.ReadDir(n.path)
 | 
			
		||||
		if err != nil {
 | 
			
		||||
			log.Error("FUSE ReadDir failed").Str("path", n.path).Err(err).Send()
 | 
			
		||||
			return nil, syscallErr(err)
 | 
			
		||||
		}
 | 
			
		||||
 | 
			
		||||
		log.Debug("FUSE ReadDir succeeded").Str("path", n.path).Int("objects", len(files)).Send()
 | 
			
		||||
		r := make([]fuse.DirEntry, len(files))
 | 
			
		||||
		n.lst = make([]DirEntry, len(files))
 | 
			
		||||
		for ind, entry := range files {
 | 
			
		||||
			info, _ := entry.Info()
 | 
			
		||||
			info, err := entry.Info()
 | 
			
		||||
			if err != nil {
 | 
			
		||||
				log.Error("FUSE Info failed").Str("path", n.path).Err(err).Send()
 | 
			
		||||
				return nil, syscallErr(err)
 | 
			
		||||
			}
 | 
			
		||||
			name := info.Name()
 | 
			
		||||
 | 
			
		||||
			file := DirEntry{
 | 
			
		||||
@@ -161,12 +175,12 @@ func (n *ITNode) Readdir(ctx context.Context) (fs.DirStream, syscall.Errno) {
 | 
			
		||||
	return fs.NewListDirStream(r), 0
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
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) {
 | 
			
		||||
	switch n.kind {
 | 
			
		||||
	case 0:
 | 
			
		||||
		// root folder
 | 
			
		||||
		if name == "device" {
 | 
			
		||||
		if name == "info" {
 | 
			
		||||
			stable := fs.StableAttr{
 | 
			
		||||
				Mode: fuse.S_IFDIR,
 | 
			
		||||
				Ino: uint64(0),
 | 
			
		||||
@@ -184,7 +198,7 @@ func (n *ITNode) Lookup(ctx context.Context, name string, out *fuse.EntryOut) (*
 | 
			
		||||
			return child, 0
 | 
			
		||||
		}
 | 
			
		||||
	case 1:
 | 
			
		||||
		// device folder
 | 
			
		||||
		// info folder
 | 
			
		||||
		for _, value := range properties {
 | 
			
		||||
			if value.name == name {
 | 
			
		||||
				stable := fs.StableAttr{
 | 
			
		||||
@@ -196,7 +210,6 @@ func (n *ITNode) Lookup(ctx context.Context, name string, out *fuse.EntryOut) (*
 | 
			
		||||
				return child, 0
 | 
			
		||||
			}
 | 
			
		||||
		}
 | 
			
		||||
		return nil, syscall.ENOENT
 | 
			
		||||
 | 
			
		||||
	case 2:
 | 
			
		||||
		// FS object
 | 
			
		||||
@@ -206,9 +219,9 @@ func (n *ITNode) Lookup(ctx context.Context, name string, out *fuse.EntryOut) (*
 | 
			
		||||
 | 
			
		||||
		for _, file := range n.lst {
 | 
			
		||||
			if file.path != n.path + "/" + name {
 | 
			
		||||
				continue;
 | 
			
		||||
				continue
 | 
			
		||||
			}
 | 
			
		||||
			log.Info("LookUp successful").Str("path", file.path).Send()
 | 
			
		||||
			log.Debug("FUSE Lookup successful").Str("path", file.path).Send()
 | 
			
		||||
 | 
			
		||||
			if file.isDir {
 | 
			
		||||
				stable := fs.StableAttr{
 | 
			
		||||
@@ -230,9 +243,9 @@ func (n *ITNode) Lookup(ctx context.Context, name string, out *fuse.EntryOut) (*
 | 
			
		||||
				child := n.NewInode(ctx, operations, stable)
 | 
			
		||||
				return child, 0
 | 
			
		||||
			}
 | 
			
		||||
			break;
 | 
			
		||||
			break
 | 
			
		||||
		}
 | 
			
		||||
		log.Warn("LookUp failed").Str("path", n.path + "/" + name).Send()
 | 
			
		||||
		log.Warn("FUSE Lookup failed").Str("path", n.path + "/" + name).Send()
 | 
			
		||||
	}
 | 
			
		||||
	return nil, syscall.ENOENT
 | 
			
		||||
}
 | 
			
		||||
@@ -240,10 +253,10 @@ func (n *ITNode) Lookup(ctx context.Context, name string, out *fuse.EntryOut) (*
 | 
			
		||||
type bytesFileReadHandle struct {
 | 
			
		||||
	content []byte
 | 
			
		||||
}
 | 
			
		||||
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) {
 | 
			
		||||
	log.Info("Executing Read").Int("size", len(fh.content)).Send()
 | 
			
		||||
	log.Debug("FUSE Executing Read").Int("size", len(fh.content)).Send()
 | 
			
		||||
	end := off + int64(len(dest))
 | 
			
		||||
	if end > int64(len(fh.content)) {
 | 
			
		||||
		end = int64(len(fh.content))
 | 
			
		||||
@@ -252,18 +265,20 @@ func (fh *bytesFileReadHandle) Read(ctx context.Context, dest []byte, off int64)
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
type sensorFileReadHandle struct {
 | 
			
		||||
	ch <-chan []byte
 | 
			
		||||
	cancel context.CancelFunc
 | 
			
		||||
	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) {
 | 
			
		||||
	content := <-fh.ch
 | 
			
		||||
	return fuse.ReadResultData(content), 0
 | 
			
		||||
	log.Info("Executing Read").Int("size", len(fh.content)).Send()
 | 
			
		||||
	end := off + int64(len(dest))
 | 
			
		||||
	if end > int64(len(fh.content)) {
 | 
			
		||||
		end = int64(len(fh.content))
 | 
			
		||||
	}
 | 
			
		||||
	return fuse.ReadResultData(fh.content[off:end]), 0
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
var _ = (fs.FileFlusher)((*sensorFileReadHandle)(nil))
 | 
			
		||||
var _ fs.FileFlusher = (*sensorFileReadHandle)(nil)
 | 
			
		||||
func (fh *sensorFileReadHandle) Flush(ctx context.Context) (errno syscall.Errno) {
 | 
			
		||||
	fh.cancel()
 | 
			
		||||
	return 0
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
@@ -273,64 +288,73 @@ type bytesFileWriteHandle struct {
 | 
			
		||||
	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) {
 | 
			
		||||
	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)) {
 | 
			
		||||
		log.Error("FUSE Write file size changed unexpectedly").Int("expect", int(off)).Int("received", len(fh.content)).Send()
 | 
			
		||||
		return 0, syscall.ENXIO
 | 
			
		||||
	}
 | 
			
		||||
	fh.content = append(fh.content[:], data[:]...)
 | 
			
		||||
	return uint32(len(data)), 0
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
var _ = (fs.FileFlusher)((*bytesFileWriteHandle)(nil))
 | 
			
		||||
var _ fs.FileFlusher = (*bytesFileWriteHandle)(nil)
 | 
			
		||||
func (fh *bytesFileWriteHandle) Flush(ctx context.Context) (errno syscall.Errno) {
 | 
			
		||||
	if len(fh.content) == 0 {
 | 
			
		||||
		return 0
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	log.Info("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)))
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		log.Error("Flush failed: create").Str("path", fh.path).Err(err).Send()
 | 
			
		||||
		return syscall.EROFS
 | 
			
		||||
		log.Error("FUSE Flush failed: create").Str("path", fh.path).Err(err).Send()
 | 
			
		||||
		return syscallErr(err)
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	if len(fh.content) == 0 {
 | 
			
		||||
		log.Debug("FUSE Flush no data to write").Str("path", fh.path).Send()
 | 
			
		||||
		err = fp.Close()
 | 
			
		||||
		if err != nil {
 | 
			
		||||
			log.Error("FUSE Flush failed during close").Str("path", fh.path).Err(err).Send()
 | 
			
		||||
			return syscallErr(err)
 | 
			
		||||
		}
 | 
			
		||||
		return 0
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	go func() {
 | 
			
		||||
		// For every progress event
 | 
			
		||||
		for sent := range fp.Progress() {
 | 
			
		||||
			log.Info("Progress").Int("bytes", int(sent)).Int("of", len(fh.content)).Send();
 | 
			
		||||
			log.Debug("FUSE Flush progress").Int("bytes", int(sent)).Int("total", len(fh.content)).Send()
 | 
			
		||||
		}
 | 
			
		||||
	}()
 | 
			
		||||
 | 
			
		||||
	r := bytes.NewReader(fh.content)
 | 
			
		||||
	nread, err := io.Copy(fp, r)
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		log.Error("Flush failed: write").Str("path", fh.path).Err(err).Send()
 | 
			
		||||
		log.Error("FUSE Flush failed during write").Str("path", fh.path).Err(err).Send()
 | 
			
		||||
		fp.Close()
 | 
			
		||||
		return syscall.EROFS
 | 
			
		||||
		return syscallErr(err)
 | 
			
		||||
	}
 | 
			
		||||
	if int(nread) != len(fh.content) {
 | 
			
		||||
		log.Error("Flush failed: write").Str("path", fh.path).Int("expect", len(fh.content)).Int("got", int(nread)).Send()
 | 
			
		||||
		log.Error("FUSE Flush failed during write").Str("path", fh.path).Int("expect", len(fh.content)).Int("got", int(nread)).Send()
 | 
			
		||||
		fp.Close()
 | 
			
		||||
		return syscall.EROFS
 | 
			
		||||
		return syscall.EIO
 | 
			
		||||
	}
 | 
			
		||||
	err = fp.Close()
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		log.Error("Flush failed: close").Str("path", fh.path).Err(err).Send()
 | 
			
		||||
		return syscall.EROFS
 | 
			
		||||
		log.Error("FUSE Flush failed during close").Str("path", fh.path).Err(err).Send()
 | 
			
		||||
		return syscallErr(err)
 | 
			
		||||
	}
 | 
			
		||||
	log.Info("Flush done").Str("path", fh.path).Int("size", len(fh.content)).Send()
 | 
			
		||||
	log.Debug("FUSE Flush done").Str("path", fh.path).Int("size", len(fh.content)).Send()
 | 
			
		||||
 | 
			
		||||
	return 0
 | 
			
		||||
}
 | 
			
		||||
var _ = (fs.FileFsyncer)((*bytesFileWriteHandle)(nil))
 | 
			
		||||
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))
 | 
			
		||||
var _ fs.NodeGetattrer = (*ITNode)(nil)
 | 
			
		||||
func (bn *ITNode) Getattr(ctx context.Context, f fs.FileHandle, out *fuse.AttrOut) syscall.Errno {
 | 
			
		||||
	log.Info("getattr").Str("path", bn.path).Send();
 | 
			
		||||
	log.Debug("FUSE getattr").Str("path", bn.path).Send()
 | 
			
		||||
	out.Ino = bn.Ino
 | 
			
		||||
	out.Mtime = bn.self.modtime
 | 
			
		||||
	out.Ctime = bn.self.modtime
 | 
			
		||||
@@ -339,37 +363,37 @@ func (bn *ITNode) Getattr(ctx context.Context, f fs.FileHandle, out *fuse.AttrOu
 | 
			
		||||
	return 0
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
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 {
 | 
			
		||||
	log.Info("setattr").Str("path", bn.path).Send()
 | 
			
		||||
	out.Size = 0;
 | 
			
		||||
	out.Mtime = 0;
 | 
			
		||||
	log.Debug("FUSE setattr").Str("path", bn.path).Send()
 | 
			
		||||
	out.Size = 0
 | 
			
		||||
	out.Mtime = 0
 | 
			
		||||
	return 0
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
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) {
 | 
			
		||||
	switch f.kind {
 | 
			
		||||
	case 2:
 | 
			
		||||
		// FS file
 | 
			
		||||
		if openFlags&syscall.O_RDWR != 0 {
 | 
			
		||||
			log.Warn("open: failed RDWR").Str("path", f.path).Send()
 | 
			
		||||
			log.Error("FUSE Open failed: RDWR").Str("path", f.path).Send()
 | 
			
		||||
			return nil, 0, syscall.EROFS
 | 
			
		||||
		}
 | 
			
		||||
 | 
			
		||||
		if openFlags & syscall.O_WRONLY != 0 {
 | 
			
		||||
			log.Info("Opening file: write").Str("path", f.path).Send()
 | 
			
		||||
			log.Debug("FUSE Opening for write").Str("path", f.path).Send()
 | 
			
		||||
			fh = &bytesFileWriteHandle{
 | 
			
		||||
				path : f.path,
 | 
			
		||||
				content : make([]byte, 0),
 | 
			
		||||
			}
 | 
			
		||||
			return fh, fuse.FOPEN_DIRECT_IO, 0
 | 
			
		||||
		} else {
 | 
			
		||||
			log.Info("Opening file: read").Str("path", f.path).Send();
 | 
			
		||||
			log.Debug("FUSE Opening for read").Str("path", f.path).Send()
 | 
			
		||||
			fp, err := myfs.Open(f.path)
 | 
			
		||||
			if err != nil {
 | 
			
		||||
				log.Error("Opening file failed").Str("path", f.path).Err(err).Send();
 | 
			
		||||
				return nil, 0, syscall.EROFS
 | 
			
		||||
				log.Error("FUSE: Opening failed").Str("path", f.path).Err(err).Send()
 | 
			
		||||
				return nil, 0, syscallErr(err)
 | 
			
		||||
			}
 | 
			
		||||
 | 
			
		||||
			defer fp.Close()
 | 
			
		||||
@@ -379,15 +403,15 @@ func (f *ITNode) Open(ctx context.Context, openFlags uint32) (fh fs.FileHandle,
 | 
			
		||||
			go func() {
 | 
			
		||||
				// For every progress event
 | 
			
		||||
				for sent := range fp.Progress() {
 | 
			
		||||
					log.Info("Progress").Int("bytes", int(sent)).Int("of", int(f.self.size)).Send();
 | 
			
		||||
					log.Debug("FUSE Read progress").Int("bytes", int(sent)).Int("total", int(f.self.size)).Send()
 | 
			
		||||
				}
 | 
			
		||||
			}()
 | 
			
		||||
 | 
			
		||||
			_, err = io.Copy(b, fp)
 | 
			
		||||
			if err != nil {
 | 
			
		||||
				log.Error("Read failed").Str("path", f.path).Err(err).Send()
 | 
			
		||||
				log.Error("FUSE Read failed").Str("path", f.path).Err(err).Send()
 | 
			
		||||
				fp.Close()
 | 
			
		||||
				return nil, 0, syscall.EROFS
 | 
			
		||||
				return nil, 0, syscallErr(err)
 | 
			
		||||
			}
 | 
			
		||||
 | 
			
		||||
			fh = &bytesFileReadHandle{
 | 
			
		||||
@@ -406,24 +430,22 @@ func (f *ITNode) Open(ctx context.Context, openFlags uint32) (fh fs.FileHandle,
 | 
			
		||||
 | 
			
		||||
		for _, value := range properties {
 | 
			
		||||
			if value.Ino == f.Ino {
 | 
			
		||||
				sub_ctx, cancel := context.WithCancel(ctx)
 | 
			
		||||
				ans, err := value.gen(sub_ctx)
 | 
			
		||||
				ans, err := value.gen()
 | 
			
		||||
				if err != nil {
 | 
			
		||||
					return nil, 0, syscall.EROFS
 | 
			
		||||
					return nil, 0, syscallErr(err)
 | 
			
		||||
				}
 | 
			
		||||
 | 
			
		||||
				fh = &sensorFileReadHandle{
 | 
			
		||||
					ch: ans,
 | 
			
		||||
					cancel : cancel,
 | 
			
		||||
					content : ans,
 | 
			
		||||
				}
 | 
			
		||||
				return fh, fuse.FOPEN_DIRECT_IO, 0
 | 
			
		||||
			}
 | 
			
		||||
		}
 | 
			
		||||
	}
 | 
			
		||||
	return nil, 0, syscall.EROFS
 | 
			
		||||
	return nil, 0, syscall.EINVAL
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
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) {
 | 
			
		||||
	if f.kind != 2 {
 | 
			
		||||
		return nil, nil, 0, syscall.EROFS
 | 
			
		||||
@@ -448,13 +470,13 @@ func (f *ITNode) Create(ctx context.Context, name string, flags uint32, mode uin
 | 
			
		||||
		content : make([]byte, 0),
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	log.Info("Creating file").Str("path", path).Send()
 | 
			
		||||
	log.Debug("FUSE Creating file").Str("path", path).Send()
 | 
			
		||||
 | 
			
		||||
	errno = 0
 | 
			
		||||
	return node, fh, fuseFlags, 0
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
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) {
 | 
			
		||||
	if f.kind != 2 {
 | 
			
		||||
		return nil, syscall.EROFS
 | 
			
		||||
@@ -463,11 +485,11 @@ func (f *ITNode) Mkdir(ctx context.Context, name string, mode uint32, out *fuse.
 | 
			
		||||
	path := f.path + "/" + name
 | 
			
		||||
	err := myfs.Mkdir(path)
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		log.Info("Mkdir failed").
 | 
			
		||||
		log.Error("FUSE Mkdir failed").
 | 
			
		||||
			Str("path", path).
 | 
			
		||||
			Err(err).
 | 
			
		||||
			Send()
 | 
			
		||||
		return nil, syscall.EROFS
 | 
			
		||||
		return nil, syscallErr(err)
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	ino := uint64(len(inodemap)) + 11
 | 
			
		||||
@@ -483,29 +505,33 @@ func (f *ITNode) Mkdir(ctx context.Context, name string, mode uint32, out *fuse.
 | 
			
		||||
	}
 | 
			
		||||
	node := f.NewInode(ctx, operations, stable)
 | 
			
		||||
 | 
			
		||||
	log.Info("Mkdir sucess").
 | 
			
		||||
	log.Debug("FUSE Mkdir success").
 | 
			
		||||
		Str("path", path).
 | 
			
		||||
		Int("ino", int(ino)).
 | 
			
		||||
		Send()
 | 
			
		||||
	return node, 0
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
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 {
 | 
			
		||||
	if f.kind != 2 {
 | 
			
		||||
		return syscall.EROFS
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	p1 := f.path + "/" + name
 | 
			
		||||
	p2 := newParent.EmbeddedInode().Path(nil)[2:] + "/" + newName
 | 
			
		||||
 | 
			
		||||
	err := myfs.Rename(p1, p2)
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		log.Error("Rename failed").
 | 
			
		||||
		log.Error("FUSE Rename failed").
 | 
			
		||||
			Str("src", p1).
 | 
			
		||||
			Str("dest", p2).
 | 
			
		||||
			Err(err).
 | 
			
		||||
			Send()
 | 
			
		||||
 | 
			
		||||
		return syscall.EROFS
 | 
			
		||||
		return syscallErr(err)
 | 
			
		||||
	}
 | 
			
		||||
	log.Info("Rename sucess").
 | 
			
		||||
	log.Debug("FUSE Rename sucess").
 | 
			
		||||
		Str("src", p1).
 | 
			
		||||
		Str("dest", p2).
 | 
			
		||||
		Send()
 | 
			
		||||
@@ -517,26 +543,30 @@ func (f *ITNode) Rename(ctx context.Context, name string, newParent fs.InodeEmbe
 | 
			
		||||
	return 0
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
var _ = (fs.NodeUnlinker)((*ITNode)(nil))
 | 
			
		||||
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)
 | 
			
		||||
	err := myfs.Remove(f.path + "/" + name)
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		log.Error("Unlink failed").
 | 
			
		||||
		log.Error("FUSE Unlink failed").
 | 
			
		||||
			Str("file", f.path + "/" + name).
 | 
			
		||||
			Err(err).
 | 
			
		||||
			Send()
 | 
			
		||||
 | 
			
		||||
		return syscall.EROFS
 | 
			
		||||
		return syscallErr(err)
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	log.Info("Unlink success").
 | 
			
		||||
	log.Debug("FUSE Unlink success").
 | 
			
		||||
		Str("file", f.path + "/" + name).
 | 
			
		||||
		Send()
 | 
			
		||||
	return 0
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
var _ = (fs.NodeRmdirer)((*ITNode)(nil))
 | 
			
		||||
var _ fs.NodeRmdirer = (*ITNode)(nil)
 | 
			
		||||
func (f *ITNode) Rmdir(ctx context.Context, name string) syscall.Errno {
 | 
			
		||||
	return f.Unlink(ctx, name)
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
							
								
								
									
										67
									
								
								internal/fusefs/syscallerr.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										67
									
								
								internal/fusefs/syscallerr.go
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,67 @@
 | 
			
		||||
package fusefs
 | 
			
		||||
import (
 | 
			
		||||
	"go.arsenm.dev/infinitime/blefs"
 | 
			
		||||
	"syscall"
 | 
			
		||||
)
 | 
			
		||||
 | 
			
		||||
func syscallErr(err error) syscall.Errno {
 | 
			
		||||
	if err == nil {
 | 
			
		||||
		return 0
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	switch err {
 | 
			
		||||
	case blefs.FSError{0x02}: // filesystem error
 | 
			
		||||
		return syscall.EIO // TODO
 | 
			
		||||
	case blefs.FSError{0x05}: // read-only filesystem
 | 
			
		||||
		return syscall.EROFS
 | 
			
		||||
	case blefs.FSError{0x03}: // no such file
 | 
			
		||||
		return syscall.ENOENT
 | 
			
		||||
	case blefs.FSError{0x04}: // protocol error
 | 
			
		||||
		return syscall.EPROTO
 | 
			
		||||
	case blefs.FSError{-5}: // input/output error
 | 
			
		||||
		return syscall.EIO
 | 
			
		||||
	case blefs.FSError{-84}: // filesystem is corrupted
 | 
			
		||||
		return syscall.ENOTRECOVERABLE // TODO
 | 
			
		||||
	case blefs.FSError{-2}: // no such directory entry
 | 
			
		||||
		return syscall.ENOENT
 | 
			
		||||
	case blefs.FSError{-17}: // entry already exists
 | 
			
		||||
		return syscall.EEXIST
 | 
			
		||||
	case blefs.FSError{-20}: // entry is not a directory
 | 
			
		||||
		return syscall.ENOTDIR
 | 
			
		||||
	case blefs.FSError{-39}: // directory is not empty
 | 
			
		||||
		return syscall.ENOTEMPTY
 | 
			
		||||
	case blefs.FSError{-9}: // bad file number
 | 
			
		||||
		return syscall.EBADF
 | 
			
		||||
	case blefs.FSError{-27}: // file is too large
 | 
			
		||||
		return syscall.EFBIG
 | 
			
		||||
	case blefs.FSError{-22}: // invalid parameter
 | 
			
		||||
		return syscall.EINVAL
 | 
			
		||||
	case blefs.FSError{-28}: // no space left on device
 | 
			
		||||
		return syscall.ENOSPC
 | 
			
		||||
	case blefs.FSError{-12}: // no more memory available
 | 
			
		||||
		return syscall.ENOMEM
 | 
			
		||||
	case blefs.FSError{-61}: // no attr available
 | 
			
		||||
		return syscall.ENODATA // TODO
 | 
			
		||||
	case blefs.FSError{-36}: // file name is too long
 | 
			
		||||
		return syscall.ENAMETOOLONG
 | 
			
		||||
	case blefs.ErrFileNotExists: // file does not exist
 | 
			
		||||
		return syscall.ENOENT
 | 
			
		||||
	case blefs.ErrFileReadOnly: // file is read only
 | 
			
		||||
		return syscall.EACCES
 | 
			
		||||
	case blefs.ErrFileWriteOnly: // file is write only
 | 
			
		||||
		return syscall.EACCES
 | 
			
		||||
	case blefs.ErrInvalidOffset: // invalid file offset
 | 
			
		||||
		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
 | 
			
		||||
		return syscall.ENFILE
 | 
			
		||||
	case blefs.ErrWriteOpen: // only one file can be opened for writing at a time
 | 
			
		||||
		return syscall.ENFILE
 | 
			
		||||
	case blefs.ErrNoRemoveRoot: // refusing to remove root directory
 | 
			
		||||
		return syscall.EPERM
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	return syscall.EIO // TODO
 | 
			
		||||
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										15
									
								
								internal/fusefs/unmount.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										15
									
								
								internal/fusefs/unmount.go
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,15 @@
 | 
			
		||||
package fusefs
 | 
			
		||||
 | 
			
		||||
import (
 | 
			
		||||
	_ "unsafe"
 | 
			
		||||
	"github.com/hanwen/go-fuse/v2/fuse"
 | 
			
		||||
)
 | 
			
		||||
 | 
			
		||||
func Unmount(mountPoint string) error {
 | 
			
		||||
	return unmount(mountPoint, &fuse.MountOptions{DirectMount: false})
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// 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
 | 
			
		||||
							
								
								
									
										15
									
								
								main.go
									
									
									
									
									
								
							
							
						
						
									
										15
									
								
								main.go
									
									
									
									
									
								
							@@ -181,18 +181,19 @@ func main() {
 | 
			
		||||
		log.Error("Error intializing puremaps integration").Err(err).Send()
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	// Start fuse socket
 | 
			
		||||
	if k.Bool("fuse.enabled") {
 | 
			
		||||
		err = startFUSE(ctx, dev)
 | 
			
		||||
		if err != nil {
 | 
			
		||||
			log.Error("Error starting fuse socket").Err(err).Send()
 | 
			
		||||
		}
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	// Start control socket
 | 
			
		||||
	err = startSocket(ctx, dev)
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		log.Error("Error starting socket").Err(err).Send()
 | 
			
		||||
	}
 | 
			
		||||
	// Start fuse socket
 | 
			
		||||
	if k.Bool("fuse.enabled") {
 | 
			
		||||
		err = startFuse(ctx, dev)
 | 
			
		||||
		if err != nil {
 | 
			
		||||
			log.Error("Error starting fuse socket").Err(err).Send()
 | 
			
		||||
		}
 | 
			
		||||
	}
 | 
			
		||||
	// Block forever
 | 
			
		||||
	select {}
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user