itd/fuse/mock.go

162 lines
3.1 KiB
Go
Raw Normal View History

2023-02-19 12:59:40 +00:00
package main
2023-02-19 13:13:02 +00:00
import (
2023-02-19 14:23:04 +00:00
"os"
"io/ioutil"
2023-02-19 13:13:02 +00:00
"errors"
2023-02-21 20:05:49 +00:00
"time"
2023-02-19 13:13:02 +00:00
)
2023-02-19 14:23:04 +00:00
const (
FSStatusOk = 0x01
FSStatusError = 0x02
)
2023-02-19 13:13:02 +00:00
var (
ErrFileNotExists = errors.New("file does not exist")
ErrFileReadOnly = errors.New("file is read only")
ErrFileWriteOnly = errors.New("file is write only")
ErrInvalidOffset = errors.New("invalid file offset")
ErrOffsetChanged = errors.New("offset has already been changed")
ErrReadOpen = errors.New("only one file can be opened for reading at a time")
ErrWriteOpen = errors.New("only one file can be opened for writing at a time")
ErrNoRemoveRoot = errors.New("refusing to remove root directory")
)
2023-02-19 12:59:40 +00:00
type Device struct {
2023-02-19 13:13:02 +00:00
}
type FS struct {
}
2023-02-19 12:59:40 +00:00
2023-02-21 20:05:49 +00:00
type MyFileInfo struct {
name string
size uint32
modtime time.Time
dir bool
}
func (n *MyFileInfo) Name() string {
return n.name
}
func (n *MyFileInfo) Size() uint32 {
return n.size
}
func (n *MyFileInfo) ModTime() time.Time {
return n.modtime
}
func (n *MyFileInfo) IsDir() bool {
return n.dir
}
type MyDirEntry struct {
self MyFileInfo
}
func (n *MyDirEntry) Info() (MyFileInfo, error) {
return n.self, nil
2023-02-19 14:23:04 +00:00
}
2023-02-19 13:13:02 +00:00
type File struct {
fs *FS
path string
2023-02-19 14:23:04 +00:00
fp *os.File
2023-02-19 13:13:02 +00:00
isReadOnly bool
isWriteOnly bool
2023-02-19 12:59:40 +00:00
}
2023-02-19 13:13:02 +00:00
2023-02-19 12:59:40 +00:00
func (i *Device) HeartRate() (uint8, error) {
return 10, nil
}
func (i *Device) BatteryLevel() (uint8, error) {
return 95, nil
}
func (i *Device) StepCount() (uint32, error) {
return 27000, nil
}
type MotionValues struct {
X int16
Y int16
Z int16
}
func (i *Device) Motion() (MotionValues, error) {
return MotionValues{-12, +64, -19}, nil
}
func (i *Device) Address() string {
return "FA:E4:00:00:00:00"
}
func (i *Device) Version() (string, error) {
return "1.11.0", nil
}
2023-02-19 13:13:02 +00:00
2023-02-21 20:05:49 +00:00
func (blefs *FS) ReadDir(path string) ([]MyDirEntry, error) {
var out []MyDirEntry
2023-02-19 14:23:04 +00:00
files, err := ioutil.ReadDir("./root/" + path)
if err != nil {
return nil, nil
}
for _, f := range files {
2023-02-21 20:05:49 +00:00
listing := MyFileInfo{}
listing.modtime = f.ModTime()
2023-02-19 14:23:04 +00:00
listing.size = uint32(f.Size())
2023-02-21 20:11:38 +00:00
listing.name = f.Name()
2023-02-21 20:05:49 +00:00
listing.dir = f.IsDir()
out = append(out, MyDirEntry{self : listing})
2023-02-19 14:23:04 +00:00
}
2023-02-19 13:13:02 +00:00
return out, nil
}
func (blefs *FS) Mkdir(path string) error {
2023-02-19 14:23:04 +00:00
return os.Mkdir("./root/" + path, os.ModePerm)
2023-02-19 13:13:02 +00:00
}
func (blefs *FS) Rename(old, new string) error {
2023-02-19 14:23:04 +00:00
return os.Rename("./root/" + old, "./root/" + new)
2023-02-19 13:13:02 +00:00
}
func (blefs *FS) Remove(path string) error {
2023-02-19 14:23:04 +00:00
return os.Remove("./root/" + path)
2023-02-19 13:13:02 +00:00
}
func (blefs *FS) Open(path string) (*File, error) {
2023-02-19 14:23:04 +00:00
fp, _ := os.Open("./root/" + path)
return &File{
fs : blefs,
path : path,
fp : fp,
isReadOnly : true,
isWriteOnly : false,
}, nil
2023-02-19 13:13:02 +00:00
}
func (blefs *FS) Create(path string, size uint32) (*File, error) {
2023-02-19 14:23:04 +00:00
fp, _ := os.Create("./root/" + path)
return &File{
fs : blefs,
path : path,
fp : fp,
isReadOnly : false,
isWriteOnly : true,
}, nil
2023-02-19 13:13:02 +00:00
}
func (fl *File) Read(b []byte) (n int, err error) {
2023-02-19 14:23:04 +00:00
if fl.isWriteOnly {
return 0, ErrFileWriteOnly
}
return fl.fp.Read(b)
2023-02-19 13:13:02 +00:00
}
func (fl *File) Write(b []byte) (n int, err error) {
2023-02-19 14:23:04 +00:00
if fl.isReadOnly {
return 0, ErrFileReadOnly
}
return fl.fp.Write(b)
2023-02-19 13:13:02 +00:00
}
func (fl *File) Close() error {
2023-02-19 14:23:04 +00:00
return fl.fp.Close()
2023-02-19 13:13:02 +00:00
}