2023-03-01 15:12:48 +00:00
|
|
|
package fusefs
|
2023-02-19 11:50:02 +00:00
|
|
|
|
|
|
|
import (
|
2023-02-26 13:05:58 +00:00
|
|
|
"go.arsenm.dev/infinitime"
|
2023-02-26 13:06:58 +00:00
|
|
|
"go.arsenm.dev/infinitime/blefs"
|
2023-02-21 19:57:36 +00:00
|
|
|
"go.arsenm.dev/logger/log"
|
2023-02-21 20:05:49 +00:00
|
|
|
"context"
|
|
|
|
"syscall"
|
2023-02-19 11:50:02 +00:00
|
|
|
"github.com/hanwen/go-fuse/v2/fs"
|
|
|
|
"github.com/hanwen/go-fuse/v2/fuse"
|
2023-02-26 13:09:41 +00:00
|
|
|
"io"
|
|
|
|
"bytes"
|
2023-02-19 11:50:02 +00:00
|
|
|
)
|
|
|
|
|
2023-02-19 12:08:56 +00:00
|
|
|
type ITProperty struct {
|
|
|
|
name string
|
|
|
|
Ino uint64
|
2023-02-26 19:07:09 +00:00
|
|
|
gen func(context.Context) (<-chan []byte, error)
|
2023-02-19 12:08:56 +00:00
|
|
|
}
|
|
|
|
|
2023-02-21 20:05:49 +00:00
|
|
|
|
|
|
|
type DirEntry struct {
|
|
|
|
isDir bool
|
|
|
|
modtime uint64
|
|
|
|
size uint32
|
|
|
|
path string
|
|
|
|
}
|
|
|
|
|
2023-02-19 11:50:02 +00:00
|
|
|
type ITNode struct {
|
|
|
|
fs.Inode
|
|
|
|
kind int
|
2023-02-19 12:23:18 +00:00
|
|
|
Ino uint64
|
2023-02-19 14:49:53 +00:00
|
|
|
|
|
|
|
lst []DirEntry
|
2023-02-19 16:54:19 +00:00
|
|
|
self DirEntry
|
2023-02-19 14:49:53 +00:00
|
|
|
path string
|
2023-02-19 11:50:02 +00:00
|
|
|
}
|
|
|
|
|
2023-03-01 18:01:25 +00:00
|
|
|
var myfs *blefs.FS = nil
|
|
|
|
var inodemap map[string]uint64 = nil
|
|
|
|
|
2023-03-01 18:08:58 +00:00
|
|
|
func BuildRootNode(dev *infinitime.Device) (*ITNode, error) {
|
|
|
|
var err error
|
2023-03-01 15:14:06 +00:00
|
|
|
inodemap = make(map[string]uint64)
|
2023-03-01 18:08:58 +00:00
|
|
|
myfs, err = dev.FS()
|
|
|
|
if err != nil {
|
|
|
|
log.Error("FUSE failed to get filesystem").Err(err).Send()
|
|
|
|
return nil, err
|
|
|
|
}
|
2023-03-01 15:14:06 +00:00
|
|
|
|
2023-03-01 18:08:58 +00:00
|
|
|
return &ITNode{kind: 0}, nil
|
2023-03-01 15:14:06 +00:00
|
|
|
}
|
|
|
|
|
2023-02-26 19:10:47 +00:00
|
|
|
var properties = make([]ITProperty, 6)
|
2023-03-01 15:14:06 +00:00
|
|
|
|
|
|
|
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
|
|
|
|
}}
|
|
|
|
properties[1] = ITProperty{"battery", 3,
|
|
|
|
func(ctx context.Context) (<-chan []byte, error) {
|
|
|
|
ans, err := dev.WatchBatteryLevel(ctx)
|
|
|
|
return converterU8(ctx, ans), err
|
|
|
|
}}
|
|
|
|
properties[2] = ITProperty{"motion", 4,
|
|
|
|
func(ctx context.Context) (<-chan []byte, error) {
|
|
|
|
ans, err := dev.WatchMotion(ctx)
|
|
|
|
return converterMotionValues(ctx, ans), err
|
|
|
|
}}
|
|
|
|
properties[3] = ITProperty{"stepcount", 5,
|
|
|
|
func(ctx context.Context) (<-chan []byte, error) {
|
|
|
|
ans, err := dev.WatchStepCount(ctx)
|
|
|
|
return converterU32(ctx, ans), err
|
|
|
|
}}
|
|
|
|
properties[4] = ITProperty{"version", 6,
|
|
|
|
func(ctx context.Context) (<-chan []byte, error) {
|
|
|
|
ans, err := dev.Version()
|
|
|
|
return converter1String(ctx, ans), err
|
|
|
|
}}
|
|
|
|
properties[5] = ITProperty{"address", 7,
|
|
|
|
func(ctx context.Context) (<-chan []byte, error) {
|
|
|
|
ans := dev.Address()
|
|
|
|
return converter1String(ctx, ans), nil
|
|
|
|
}}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2023-02-19 11:50:02 +00:00
|
|
|
var _ = (fs.NodeReaddirer)((*ITNode)(nil))
|
|
|
|
|
|
|
|
// Readdir is part of the NodeReaddirer interface
|
|
|
|
func (n *ITNode) Readdir(ctx context.Context) (fs.DirStream, syscall.Errno) {
|
2023-02-19 12:08:45 +00:00
|
|
|
switch n.kind {
|
|
|
|
case 0:
|
2023-02-19 11:50:02 +00:00
|
|
|
// root folder
|
|
|
|
r := make([]fuse.DirEntry, 2)
|
|
|
|
r[0] = fuse.DirEntry{
|
|
|
|
Name: "device",
|
|
|
|
Ino: 0,
|
|
|
|
Mode: fuse.S_IFDIR,
|
|
|
|
}
|
|
|
|
r[1] = fuse.DirEntry{
|
|
|
|
Name: "fs",
|
|
|
|
Ino: 1,
|
|
|
|
Mode: fuse.S_IFDIR,
|
|
|
|
}
|
|
|
|
return fs.NewListDirStream(r), 0
|
2023-02-19 12:08:56 +00:00
|
|
|
|
|
|
|
case 1:
|
|
|
|
// device folder
|
|
|
|
r := make([]fuse.DirEntry, 6)
|
|
|
|
for ind, value := range properties {
|
|
|
|
r[ind] = fuse.DirEntry{
|
|
|
|
Name: value.name,
|
|
|
|
Ino: value.Ino,
|
|
|
|
Mode: fuse.S_IFREG,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return fs.NewListDirStream(r), 0
|
2023-02-19 14:49:53 +00:00
|
|
|
|
|
|
|
case 2:
|
|
|
|
// on device
|
2023-03-01 18:08:58 +00:00
|
|
|
files, err := myfs.ReadDir(n.path)
|
|
|
|
if err != nil {
|
|
|
|
log.Error("ReadDir failed").Str("path", n.path).Err(err).Send()
|
|
|
|
return nil, syscall.ENOENT
|
|
|
|
}
|
|
|
|
|
2023-02-21 19:57:36 +00:00
|
|
|
log.Info("readdir").Str("path", n.path).Int("objects", len(files)).Send()
|
2023-02-19 14:49:53 +00:00
|
|
|
r := make([]fuse.DirEntry, len(files))
|
2023-02-21 20:05:49 +00:00
|
|
|
n.lst = make([]DirEntry, len(files))
|
|
|
|
for ind, entry := range files {
|
2023-03-01 18:08:58 +00:00
|
|
|
info, err := entry.Info()
|
|
|
|
if err != nil {
|
|
|
|
log.Error("Info failed").Str("path", n.path).Err(err).Send()
|
|
|
|
return nil, syscall.ENOENT
|
|
|
|
}
|
2023-02-21 20:05:49 +00:00
|
|
|
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
|
2023-02-19 14:49:53 +00:00
|
|
|
|
|
|
|
ino := inodemap[file.path]
|
|
|
|
if ino == 0 {
|
|
|
|
ino = uint64(len(inodemap)) + 1
|
|
|
|
inodemap[file.path] = ino
|
|
|
|
}
|
|
|
|
|
2023-02-21 20:05:49 +00:00
|
|
|
if file.isDir {
|
2023-02-19 14:49:53 +00:00
|
|
|
r[ind] = fuse.DirEntry{
|
|
|
|
Name: name,
|
|
|
|
Mode: fuse.S_IFDIR,
|
|
|
|
Ino : ino + 10,
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
r[ind] = fuse.DirEntry{
|
|
|
|
Name: name,
|
|
|
|
Mode: fuse.S_IFREG,
|
|
|
|
Ino : ino + 10,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return fs.NewListDirStream(r), 0
|
2023-02-19 11:50:02 +00:00
|
|
|
}
|
|
|
|
r := make([]fuse.DirEntry, 0)
|
|
|
|
return fs.NewListDirStream(r), 0
|
|
|
|
}
|
|
|
|
|
|
|
|
var _ = (fs.NodeLookuper)((*ITNode)(nil))
|
|
|
|
func (n *ITNode) Lookup(ctx context.Context, name string, out *fuse.EntryOut) (*fs.Inode, syscall.Errno) {
|
2023-02-19 12:08:45 +00:00
|
|
|
switch n.kind {
|
|
|
|
case 0:
|
|
|
|
// root folder
|
2023-02-19 11:50:02 +00:00
|
|
|
if name == "device" {
|
|
|
|
stable := fs.StableAttr{
|
|
|
|
Mode: fuse.S_IFDIR,
|
|
|
|
Ino: uint64(0),
|
|
|
|
}
|
2023-02-19 12:23:18 +00:00
|
|
|
operations := &ITNode{kind: 1, Ino: 0}
|
2023-02-19 11:50:02 +00:00
|
|
|
child := n.NewInode(ctx, operations, stable)
|
|
|
|
return child, 0
|
|
|
|
} else if name == "fs" {
|
|
|
|
stable := fs.StableAttr{
|
|
|
|
Mode: fuse.S_IFDIR,
|
|
|
|
Ino: uint64(1),
|
|
|
|
}
|
2023-02-19 14:49:53 +00:00
|
|
|
operations := &ITNode{kind: 2, Ino: 1, path : ""}
|
2023-02-19 11:50:02 +00:00
|
|
|
child := n.NewInode(ctx, operations, stable)
|
|
|
|
return child, 0
|
|
|
|
}
|
2023-02-19 12:08:56 +00:00
|
|
|
case 1:
|
|
|
|
// device folder
|
|
|
|
for _, value := range properties {
|
|
|
|
if value.name == name {
|
|
|
|
stable := fs.StableAttr{
|
|
|
|
Mode: fuse.S_IFREG,
|
|
|
|
Ino: uint64(value.Ino),
|
|
|
|
}
|
2023-02-19 12:23:18 +00:00
|
|
|
operations := &ITNode{kind: 3, Ino: value.Ino}
|
2023-02-19 12:08:56 +00:00
|
|
|
child := n.NewInode(ctx, operations, stable)
|
|
|
|
return child, 0
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil, syscall.ENOENT
|
2023-02-19 14:49:53 +00:00
|
|
|
|
|
|
|
case 2:
|
|
|
|
// FS object
|
2023-02-26 13:15:09 +00:00
|
|
|
if len(n.lst) == 0 {
|
|
|
|
n.Readdir(ctx)
|
|
|
|
}
|
|
|
|
|
2023-02-19 14:49:53 +00:00
|
|
|
for _, file := range n.lst {
|
|
|
|
if file.path != n.path + "/" + name {
|
2023-03-01 18:01:25 +00:00
|
|
|
continue
|
2023-02-19 14:49:53 +00:00
|
|
|
}
|
2023-02-21 19:57:36 +00:00
|
|
|
log.Info("LookUp successful").Str("path", file.path).Send()
|
2023-02-19 14:49:53 +00:00
|
|
|
|
2023-02-21 20:05:49 +00:00
|
|
|
if file.isDir {
|
2023-02-19 14:49:53 +00:00
|
|
|
stable := fs.StableAttr{
|
|
|
|
Mode: fuse.S_IFDIR,
|
|
|
|
Ino: inodemap[file.path],
|
|
|
|
}
|
|
|
|
operations := &ITNode{kind: 2, path: file.path}
|
|
|
|
child := n.NewInode(ctx, operations, stable)
|
|
|
|
return child, 0
|
|
|
|
} else {
|
|
|
|
stable := fs.StableAttr{
|
|
|
|
Mode: fuse.S_IFREG,
|
|
|
|
Ino: inodemap[file.path],
|
|
|
|
}
|
2023-02-19 16:54:19 +00:00
|
|
|
operations := &ITNode{
|
|
|
|
kind: 2, path: file.path,
|
|
|
|
self: file,
|
|
|
|
}
|
2023-02-19 14:49:53 +00:00
|
|
|
child := n.NewInode(ctx, operations, stable)
|
|
|
|
return child, 0
|
|
|
|
}
|
2023-03-01 18:01:25 +00:00
|
|
|
break
|
2023-02-19 14:49:53 +00:00
|
|
|
}
|
2023-02-21 19:57:36 +00:00
|
|
|
log.Warn("LookUp failed").Str("path", n.path + "/" + name).Send()
|
2023-02-19 11:50:02 +00:00
|
|
|
}
|
|
|
|
return nil, syscall.ENOENT
|
|
|
|
}
|
|
|
|
|
2023-02-19 19:00:20 +00:00
|
|
|
type bytesFileReadHandle struct {
|
2023-02-19 12:23:44 +00:00
|
|
|
content []byte
|
|
|
|
}
|
2023-02-19 19:00:20 +00:00
|
|
|
var _ = (fs.FileReader)((*bytesFileReadHandle)(nil))
|
2023-02-19 12:23:44 +00:00
|
|
|
|
2023-02-19 19:00:20 +00:00
|
|
|
func (fh *bytesFileReadHandle) Read(ctx context.Context, dest []byte, off int64) (fuse.ReadResult, syscall.Errno) {
|
2023-02-21 19:57:36 +00:00
|
|
|
log.Info("Executing Read").Int("size", len(fh.content)).Send()
|
2023-02-19 12:23:44 +00:00
|
|
|
end := off + int64(len(dest))
|
|
|
|
if end > int64(len(fh.content)) {
|
|
|
|
end = int64(len(fh.content))
|
|
|
|
}
|
|
|
|
return fuse.ReadResultData(fh.content[off:end]), 0
|
|
|
|
}
|
|
|
|
|
2023-02-26 19:02:23 +00:00
|
|
|
type sensorFileReadHandle struct {
|
2023-02-26 19:07:09 +00:00
|
|
|
ch <-chan []byte
|
2023-02-26 19:06:11 +00:00
|
|
|
cancel context.CancelFunc
|
2023-02-26 19:02:23 +00:00
|
|
|
}
|
|
|
|
var _ = (fs.FileReader)((*sensorFileReadHandle)(nil))
|
|
|
|
func (fh *sensorFileReadHandle) Read(ctx context.Context, dest []byte, off int64) (fuse.ReadResult, syscall.Errno) {
|
2023-02-26 19:07:09 +00:00
|
|
|
content := <-fh.ch
|
2023-02-26 19:04:47 +00:00
|
|
|
return fuse.ReadResultData(content), 0
|
2023-02-26 19:02:23 +00:00
|
|
|
}
|
|
|
|
|
2023-02-26 19:06:11 +00:00
|
|
|
var _ = (fs.FileFlusher)((*sensorFileReadHandle)(nil))
|
|
|
|
func (fh *sensorFileReadHandle) Flush(ctx context.Context) (errno syscall.Errno) {
|
|
|
|
fh.cancel()
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
|
2023-02-26 19:02:23 +00:00
|
|
|
|
2023-02-19 19:00:20 +00:00
|
|
|
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) {
|
2023-02-21 20:12:10 +00:00
|
|
|
log.Info("Executing Write").Str("path", fh.path).Int("prev_size", len(fh.content)).Int("next_size", len(data)).Send()
|
2023-02-19 19:00:20 +00:00
|
|
|
if off != int64(len(fh.content)) {
|
|
|
|
}
|
|
|
|
fh.content = append(fh.content[:], data[:]...)
|
|
|
|
return uint32(len(data)), 0
|
|
|
|
}
|
|
|
|
|
|
|
|
var _ = (fs.FileFlusher)((*bytesFileWriteHandle)(nil))
|
|
|
|
func (fh *bytesFileWriteHandle) Flush(ctx context.Context) (errno syscall.Errno) {
|
2023-02-26 13:08:14 +00:00
|
|
|
if len(fh.content) == 0 {
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
|
2023-02-21 19:57:36 +00:00
|
|
|
log.Info("Attempting flush").Str("path", fh.path).Send()
|
2023-02-19 19:00:20 +00:00
|
|
|
fp, err := myfs.Create(fh.path, uint32(len(fh.content)))
|
|
|
|
if err != nil {
|
2023-02-21 19:57:36 +00:00
|
|
|
log.Error("Flush failed: create").Str("path", fh.path).Err(err).Send()
|
2023-02-19 19:00:20 +00:00
|
|
|
return syscall.EROFS
|
|
|
|
}
|
2023-02-26 13:09:41 +00:00
|
|
|
|
|
|
|
go func() {
|
|
|
|
// For every progress event
|
|
|
|
for sent := range fp.Progress() {
|
2023-03-01 18:01:25 +00:00
|
|
|
log.Info("Progress").Int("bytes", int(sent)).Int("of", len(fh.content)).Send()
|
2023-02-26 13:09:41 +00:00
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
r := bytes.NewReader(fh.content)
|
|
|
|
nread, err := io.Copy(fp, r)
|
|
|
|
if err != nil {
|
2023-02-21 19:57:36 +00:00
|
|
|
log.Error("Flush failed: write").Str("path", fh.path).Err(err).Send()
|
|
|
|
fp.Close()
|
2023-02-19 19:00:20 +00:00
|
|
|
return syscall.EROFS
|
|
|
|
}
|
2023-02-26 13:08:14 +00:00
|
|
|
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()
|
|
|
|
fp.Close()
|
|
|
|
return syscall.EROFS
|
|
|
|
}
|
2023-02-21 19:58:02 +00:00
|
|
|
err = fp.Close()
|
|
|
|
if err != nil {
|
|
|
|
log.Error("Flush failed: close").Str("path", fh.path).Err(err).Send()
|
|
|
|
return syscall.EROFS
|
|
|
|
}
|
2023-02-21 19:57:36 +00:00
|
|
|
log.Info("Flush done").Str("path", fh.path).Int("size", len(fh.content)).Send()
|
2023-02-19 19:00:20 +00:00
|
|
|
|
|
|
|
return 0
|
|
|
|
}
|
2023-02-19 20:12:38 +00:00
|
|
|
var _ = (fs.FileFsyncer)((*bytesFileWriteHandle)(nil))
|
|
|
|
func (fh *bytesFileWriteHandle) Fsync(ctx context.Context, flags uint32) (errno syscall.Errno) {
|
|
|
|
return fh.Flush(ctx)
|
|
|
|
}
|
2023-02-19 19:00:20 +00:00
|
|
|
|
2023-02-19 19:11:46 +00:00
|
|
|
var _ = (fs.NodeGetattrer)((*ITNode)(nil))
|
|
|
|
func (bn *ITNode) Getattr(ctx context.Context, f fs.FileHandle, out *fuse.AttrOut) syscall.Errno {
|
2023-03-01 18:01:25 +00:00
|
|
|
log.Info("getattr").Str("path", bn.path).Send()
|
2023-02-19 19:11:46 +00:00
|
|
|
out.Ino = bn.Ino
|
|
|
|
out.Mtime = bn.self.modtime
|
|
|
|
out.Ctime = bn.self.modtime
|
|
|
|
out.Atime = bn.self.modtime
|
|
|
|
out.Size = uint64(bn.self.size)
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
|
2023-02-19 19:00:20 +00:00
|
|
|
var _ = (fs.NodeSetattrer)((*ITNode)(nil))
|
|
|
|
func (bn *ITNode) Setattr(ctx context.Context, fh fs.FileHandle, in *fuse.SetAttrIn, out *fuse.AttrOut) syscall.Errno {
|
2023-02-21 19:57:36 +00:00
|
|
|
log.Info("setattr").Str("path", bn.path).Send()
|
2023-03-01 18:01:25 +00:00
|
|
|
out.Size = 0
|
|
|
|
out.Mtime = 0
|
2023-02-19 19:00:20 +00:00
|
|
|
return 0
|
|
|
|
}
|
|
|
|
|
2023-02-19 12:23:44 +00:00
|
|
|
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 {
|
2023-02-19 16:54:19 +00:00
|
|
|
case 2:
|
|
|
|
// FS file
|
2023-02-19 19:00:20 +00:00
|
|
|
if openFlags&syscall.O_RDWR != 0 {
|
2023-02-21 19:57:36 +00:00
|
|
|
log.Warn("open: failed RDWR").Str("path", f.path).Send()
|
2023-02-19 16:54:19 +00:00
|
|
|
return nil, 0, syscall.EROFS
|
|
|
|
}
|
|
|
|
|
2023-02-19 19:00:20 +00:00
|
|
|
if openFlags & syscall.O_WRONLY != 0 {
|
2023-02-21 19:57:36 +00:00
|
|
|
log.Info("Opening file: write").Str("path", f.path).Send()
|
2023-02-19 19:00:20 +00:00
|
|
|
fh = &bytesFileWriteHandle{
|
|
|
|
path : f.path,
|
|
|
|
content : make([]byte, 0),
|
|
|
|
}
|
|
|
|
return fh, fuse.FOPEN_DIRECT_IO, 0
|
|
|
|
} else {
|
2023-03-01 18:01:25 +00:00
|
|
|
log.Info("Opening file: read").Str("path", f.path).Send()
|
2023-02-19 19:00:20 +00:00
|
|
|
fp, err := myfs.Open(f.path)
|
|
|
|
if err != nil {
|
2023-03-01 18:01:25 +00:00
|
|
|
log.Error("Opening file failed").Str("path", f.path).Err(err).Send()
|
2023-02-19 19:00:20 +00:00
|
|
|
return nil, 0, syscall.EROFS
|
|
|
|
}
|
2023-02-19 16:54:19 +00:00
|
|
|
|
2023-02-26 13:09:00 +00:00
|
|
|
defer fp.Close()
|
|
|
|
|
2023-02-26 13:09:41 +00:00
|
|
|
b := &bytes.Buffer{}
|
|
|
|
|
|
|
|
go func() {
|
|
|
|
// For every progress event
|
|
|
|
for sent := range fp.Progress() {
|
2023-03-01 18:01:25 +00:00
|
|
|
log.Info("Progress").Int("bytes", int(sent)).Int("of", int(f.self.size)).Send()
|
2023-02-26 13:09:41 +00:00
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
_, err = io.Copy(b, fp)
|
|
|
|
if err != nil {
|
|
|
|
log.Error("Read failed").Str("path", f.path).Err(err).Send()
|
2023-02-21 19:58:02 +00:00
|
|
|
fp.Close()
|
|
|
|
return nil, 0, syscall.EROFS
|
|
|
|
}
|
2023-02-19 19:00:20 +00:00
|
|
|
|
|
|
|
fh = &bytesFileReadHandle{
|
2023-02-26 13:09:41 +00:00
|
|
|
content: b.Bytes(),
|
2023-02-19 19:00:20 +00:00
|
|
|
}
|
|
|
|
return fh, fuse.FOPEN_DIRECT_IO, 0
|
2023-02-19 16:54:19 +00:00
|
|
|
}
|
|
|
|
|
2023-02-19 12:23:44 +00:00
|
|
|
case 3:
|
|
|
|
// Device file
|
|
|
|
|
|
|
|
// disallow writes
|
2023-02-19 19:00:20 +00:00
|
|
|
if openFlags&(syscall.O_RDWR|syscall.O_WRONLY) != 0 {
|
2023-02-19 12:23:44 +00:00
|
|
|
return nil, 0, syscall.EROFS
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, value := range properties {
|
|
|
|
if value.Ino == f.Ino {
|
2023-02-26 19:06:11 +00:00
|
|
|
sub_ctx, cancel := context.WithCancel(ctx)
|
|
|
|
ans, err := value.gen(sub_ctx)
|
2023-02-19 12:59:40 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, 0, syscall.EROFS
|
|
|
|
}
|
|
|
|
|
2023-02-26 19:03:27 +00:00
|
|
|
fh = &sensorFileReadHandle{
|
|
|
|
ch: ans,
|
2023-02-26 19:06:11 +00:00
|
|
|
cancel : cancel,
|
2023-02-19 12:23:44 +00:00
|
|
|
}
|
|
|
|
return fh, fuse.FOPEN_DIRECT_IO, 0
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil, 0, syscall.EROFS
|
|
|
|
}
|
2023-02-19 11:50:02 +00:00
|
|
|
|
2023-02-19 19:39:26 +00:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
path := f.path + "/" + name
|
|
|
|
ino := uint64(len(inodemap)) + 11
|
|
|
|
inodemap[path] = ino
|
|
|
|
|
|
|
|
stable := fs.StableAttr{
|
|
|
|
Mode: fuse.S_IFREG,
|
|
|
|
Ino: ino,
|
|
|
|
}
|
|
|
|
operations := &ITNode{
|
|
|
|
kind: 2, Ino: ino,
|
|
|
|
path : path,
|
|
|
|
}
|
|
|
|
node = f.NewInode(ctx, operations, stable)
|
|
|
|
|
|
|
|
fh = &bytesFileWriteHandle{
|
|
|
|
path : path,
|
|
|
|
content : make([]byte, 0),
|
|
|
|
}
|
|
|
|
|
2023-02-21 19:57:36 +00:00
|
|
|
log.Info("Creating file").Str("path", path).Send()
|
|
|
|
|
2023-02-19 19:39:26 +00:00
|
|
|
errno = 0
|
|
|
|
return node, fh, fuseFlags, 0
|
|
|
|
}
|
|
|
|
|
2023-02-19 19:46:38 +00:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
path := f.path + "/" + name
|
|
|
|
err := myfs.Mkdir(path)
|
|
|
|
if err != nil {
|
2023-02-21 19:57:36 +00:00
|
|
|
log.Info("Mkdir failed").
|
|
|
|
Str("path", path).
|
|
|
|
Err(err).
|
|
|
|
Send()
|
2023-02-19 19:46:38 +00:00
|
|
|
return nil, syscall.EROFS
|
|
|
|
}
|
|
|
|
|
|
|
|
ino := uint64(len(inodemap)) + 11
|
|
|
|
inodemap[path] = ino
|
|
|
|
|
|
|
|
stable := fs.StableAttr{
|
|
|
|
Mode: fuse.S_IFDIR,
|
|
|
|
Ino: ino,
|
|
|
|
}
|
|
|
|
operations := &ITNode{
|
|
|
|
kind: 2, Ino: ino,
|
|
|
|
path : path,
|
|
|
|
}
|
|
|
|
node := f.NewInode(ctx, operations, stable)
|
2023-02-21 19:57:36 +00:00
|
|
|
|
|
|
|
log.Info("Mkdir sucess").
|
|
|
|
Str("path", path).
|
|
|
|
Int("ino", int(ino)).
|
|
|
|
Send()
|
2023-02-19 19:46:38 +00:00
|
|
|
return node, 0
|
|
|
|
}
|
|
|
|
|
2023-02-19 19:58:34 +00:00
|
|
|
var _ = (fs.NodeRenamer)((*ITNode)(nil))
|
|
|
|
func (f *ITNode) Rename(ctx context.Context, name string, newParent fs.InodeEmbedder, newName string, flags uint32) syscall.Errno {
|
|
|
|
p1 := f.path + "/" + name
|
|
|
|
p2 := newParent.EmbeddedInode().Path(nil)[2:] + "/" + newName
|
|
|
|
|
|
|
|
err := myfs.Rename(p1, p2)
|
|
|
|
if err != nil {
|
2023-02-21 19:57:36 +00:00
|
|
|
log.Error("Rename failed").
|
|
|
|
Str("src", p1).
|
|
|
|
Str("dest", p2).
|
|
|
|
Err(err).
|
|
|
|
Send()
|
|
|
|
|
2023-02-19 19:58:34 +00:00
|
|
|
return syscall.EROFS
|
|
|
|
}
|
2023-02-21 19:57:36 +00:00
|
|
|
log.Info("Rename sucess").
|
|
|
|
Str("src", p1).
|
|
|
|
Str("dest", p2).
|
|
|
|
Send()
|
2023-02-19 19:58:34 +00:00
|
|
|
|
|
|
|
ino := inodemap[p1]
|
|
|
|
delete(inodemap, p1)
|
|
|
|
inodemap[p2] = ino
|
|
|
|
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
|
2023-02-19 20:02:20 +00:00
|
|
|
var _ = (fs.NodeUnlinker)((*ITNode)(nil))
|
|
|
|
func (f *ITNode) Unlink(ctx context.Context, name string) syscall.Errno {
|
|
|
|
delete(inodemap, f.path + "/" + name)
|
|
|
|
err := myfs.Remove(f.path + "/" + name)
|
|
|
|
if err != nil {
|
2023-02-21 19:57:36 +00:00
|
|
|
log.Error("Unlink failed").
|
|
|
|
Str("file", f.path + "/" + name).
|
|
|
|
Err(err).
|
|
|
|
Send()
|
|
|
|
|
2023-02-19 20:02:20 +00:00
|
|
|
return syscall.EROFS
|
|
|
|
}
|
2023-02-21 19:57:36 +00:00
|
|
|
|
|
|
|
log.Info("Unlink success").
|
|
|
|
Str("file", f.path + "/" + name).
|
|
|
|
Send()
|
2023-02-19 20:02:20 +00:00
|
|
|
return 0
|
|
|
|
}
|
|
|
|
|
|
|
|
var _ = (fs.NodeRmdirer)((*ITNode)(nil))
|
|
|
|
func (f *ITNode) Rmdir(ctx context.Context, name string) syscall.Errno {
|
|
|
|
return f.Unlink(ctx, name)
|
|
|
|
}
|