forked from Elara6331/itd
		
	Run formatter
This commit is contained in:
		
							
								
								
									
										11
									
								
								fuse.go
									
									
									
									
									
								
							
							
						
						
									
										11
									
								
								fuse.go
									
									
									
									
									
								
							| @@ -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"). | ||||
|   | ||||
| @@ -1,16 +1,17 @@ | ||||
| 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 { | ||||
| @@ -19,7 +20,6 @@ type ITProperty struct { | ||||
| 	gen  func() ([]byte, error) | ||||
| } | ||||
|  | ||||
|  | ||||
| type DirEntry struct { | ||||
| 	isDir   bool | ||||
| 	modtime uint64 | ||||
| @@ -37,8 +37,10 @@ type ITNode struct { | ||||
| 	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,39 +57,49 @@ 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) | ||||
|  | ||||
| @@ -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: | ||||
| @@ -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 | ||||
| } | ||||
|  | ||||
| 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: | ||||
| @@ -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 | ||||
| @@ -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 | ||||
| @@ -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,6 +569,7 @@ 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 | ||||
| @@ -567,6 +593,7 @@ func (f *ITNode) Unlink(ctx context.Context, name string) syscall.Errno { | ||||
| } | ||||
|  | ||||
| var _ fs.NodeRmdirer = (*ITNode)(nil) | ||||
|  | ||||
| func (f *ITNode) Rmdir(ctx context.Context, name string) syscall.Errno { | ||||
| 	return f.Unlink(ctx, name) | ||||
| } | ||||
|   | ||||
| @@ -1,7 +1,9 @@ | ||||
| package fusefs | ||||
|  | ||||
| import ( | ||||
| 	"go.arsenm.dev/infinitime/blefs" | ||||
| 	"syscall" | ||||
|  | ||||
| 	"go.arsenm.dev/infinitime/blefs" | ||||
| ) | ||||
|  | ||||
| func syscallErr(err error) syscall.Errno { | ||||
| @@ -63,5 +65,4 @@ func syscallErr(err error) syscall.Errno { | ||||
| 	} | ||||
|  | ||||
| 	return syscall.EIO // TODO | ||||
|  | ||||
| } | ||||
|   | ||||
| @@ -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 | ||||
|   | ||||
		Reference in New Issue
	
	Block a user