go-vise

Constrained Size Output Virtual Machine
Info | Log | Files | Refs | README | LICENSE

commit 46241dbcdbebb9a14b309a95fc05d91f1de60d17
parent 7d0d3d0566bf831e0e4a4e2aa53493807c06d30a
Author: lash <dev@holbrook.no>
Date:   Fri, 30 Aug 2024 03:07:53 +0100

Add fs db backend, prevent invalid flag in pp

Diffstat:
Masm/flag.go | 5+++++
Adb/db.go | 26++++++++++++++++++++++++++
Adb/fs.go | 48++++++++++++++++++++++++++++++++++++++++++++++++
Adb/fs_test.go | 34++++++++++++++++++++++++++++++++++
4 files changed, 113 insertions(+), 0 deletions(-)

diff --git a/asm/flag.go b/asm/flag.go @@ -6,6 +6,8 @@ import ( "io" "os" "strconv" + + "git.defalsify.org/vise.git/state" ) // FlagParser is used to resolve flag strings to corresponding @@ -100,6 +102,9 @@ func(pp *FlagParser) Load(fp string) (int, error) { if err != nil { return 0, fmt.Errorf("Flag translation value must be numeric") } + if vv < state.FLAG_USERSTART { + return 0, fmt.Errorf("Minimum flag value is FLAG_USERSTART (%d)", FLAG_USERSTART) + } fl := uint32(vv) pp.flag[v[1]] = v[2] if fl > pp.hi { diff --git a/db/db.go b/db/db.go @@ -0,0 +1,26 @@ +package db + +import ( + "git.defalsify.org/vise.git/lang" +) + +const ( + DATATYPE_UNKNOWN = iota + DATATYPE_BIN + DATATYPE_TEMPLATE + DATATYPE_STATE +) + +type Db interface { + Connect(connStr string) error + Get(key []byte) ([]byte, error) + Put(key []byte, val []byte) error +} + +func ToDbKey(typ uint8, s string, l *lang.Language) []byte { + k := []byte{typ} + if l != nil && l.Code != "" { + s += "_" + l.Code + } + return append(k, []byte(s)...) +} diff --git a/db/fs.go b/db/fs.go @@ -0,0 +1,48 @@ +package db + +import ( + "fmt" + "io/ioutil" + "os" + "path" +) + +type FsDb struct { + ready bool + dir string +} + +func(fds *FsDb) Connect(connStr string) error { + fi, err := os.Stat(connStr) + if err != nil { + return err + } + if !fi.IsDir() { + return fmt.Errorf("fs db %s is not a directory", connStr) + } + fds.dir = connStr + return nil +} + +func(fsd *FsDb) pathFor(key []byte) string{ + return path.Join(fsd.dir, string(key)) +} + +func(fsd *FsDb) Get(key []byte) ([]byte, error) { + fp := fsd.pathFor(key) + f, err := os.Open(fp) + if err != nil { + return nil, err + } + defer f.Close() + b, err := ioutil.ReadAll(f) + if err != nil { + return nil, err + } + return b, nil +} + +func(fsd *FsDb) Put(key []byte, val []byte) error { + fp := fsd.pathFor(key) + return ioutil.WriteFile(fp, val, 0600) +} diff --git a/db/fs_test.go b/db/fs_test.go @@ -0,0 +1,34 @@ +package db + +import ( + "bytes" + "io/ioutil" + "testing" +) + +func TestPutGet(t *testing.T) { + d, err := ioutil.TempDir("", "vise-db-*") + if err != nil { + t.Fatal(err) + } + db := &FsDb{} + err = db.Connect(d) + if err != nil { + t.Fatal(err) + } + err = db.Put([]byte("foo"), []byte("bar")) + if err != nil { + t.Fatal(err) + } + v, err := db.Get([]byte("foo")) + if err != nil { + t.Fatal(err) + } + if !bytes.Equal(v, []byte("bar")) { + t.Fatalf("expected value 'bar', found '%s'", v) + } + _, err = db.Get([]byte("bar")) + if err == nil { + t.Fatal("expected get error for key 'bar'") + } +}