Implemented mocks

This commit is contained in:
Yannick Ulrich 2023-02-19 14:23:04 +00:00
parent e4f8d0b551
commit a5d70e373c

View File

@ -1,9 +1,15 @@
package main package main
import ( import (
"io/fs" "os"
"io/ioutil"
"errors" "errors"
) )
const (
FSStatusOk = 0x01
FSStatusError = 0x02
)
var ( var (
ErrFileNotExists = errors.New("file does not exist") ErrFileNotExists = errors.New("file does not exist")
ErrFileReadOnly = errors.New("file is read only") ErrFileReadOnly = errors.New("file is read only")
@ -20,18 +26,20 @@ type Device struct {
type FS struct { type FS struct {
} }
type DirEntry struct {
status int8
flags uint32
modtime uint64
size uint32
path string
}
type File struct { type File struct {
fs *FS fs *FS
path string path string
offset uint32 fp *os.File
offsetCh chan uint32
length uint32
amtLeft uint32
amtTferd uint32
isReadOnly bool isReadOnly bool
isWriteOnly bool isWriteOnly bool
offsetChanged bool
} }
@ -66,41 +74,74 @@ func (i *Device) Version() (string, error) {
return "1.11.0", nil return "1.11.0", nil
} }
func (blefs *FS) ReadDir(path string) ([]fs.DirEntry, error) { func (blefs *FS) ReadDir(path string) ([]DirEntry, error) {
var out []fs.DirEntry var out []DirEntry
files, err := ioutil.ReadDir("./root/" + path)
if err != nil {
return nil, nil
}
for _, f := range files {
listing := DirEntry{}
listing.status = FSStatusOk
listing.modtime = uint64(f.ModTime().Unix())
listing.size = uint32(f.Size())
listing.path = path + "/" + f.Name()
if f.IsDir() {
listing.flags = 1;
} else {
listing.flags = 0;
}
out = append(out, listing)
}
return out, nil return out, nil
} }
func (blefs *FS) Mkdir(path string) error { func (blefs *FS) Mkdir(path string) error {
return nil return os.Mkdir("./root/" + path, os.ModePerm)
}
func (blefs *FS) Stat(path string) (fs.FileInfo, error) {
return nil, ErrFileNotExists
} }
func (blefs *FS) Rename(old, new string) error { func (blefs *FS) Rename(old, new string) error {
return nil return os.Rename("./root/" + old, "./root/" + new)
} }
func (blefs *FS) Remove(path string) error { func (blefs *FS) Remove(path string) error {
return nil return os.Remove("./root/" + path)
} }
func (blefs *FS) Open(path string) (*File, error) { func (blefs *FS) Open(path string) (*File, error) {
return &File{}, nil fp, _ := os.Open("./root/" + path)
return &File{
fs : blefs,
path : path,
fp : fp,
isReadOnly : true,
isWriteOnly : false,
}, nil
} }
func (blefs *FS) Create(path string, size uint32) (*File, error) { func (blefs *FS) Create(path string, size uint32) (*File, error) {
return &File{}, nil fp, _ := os.Create("./root/" + path)
return &File{
fs : blefs,
path : path,
fp : fp,
isReadOnly : false,
isWriteOnly : true,
}, nil
} }
func (fl *File) Read(b []byte) (n int, err error) { func (fl *File) Read(b []byte) (n int, err error) {
return 0, ErrFileWriteOnly if fl.isWriteOnly {
return 0, ErrFileWriteOnly
}
return fl.fp.Read(b)
} }
func (fl *File) Write(b []byte) (n int, err error) { func (fl *File) Write(b []byte) (n int, err error) {
return 0, ErrFileWriteOnly if fl.isReadOnly {
return 0, ErrFileReadOnly
}
return fl.fp.Write(b)
} }
func (fl *File) Close() error { func (fl *File) Close() error {
return nil return fl.fp.Close()
} }
// func (fl *File) Stat() (fs.FileInfo, error) {
// }