mirror of
https://codeberg.org/scip/anydb.git
synced 2025-12-17 04:20:59 +01:00
Fixes and additions: (#20)
- fix encryption bug #19, which was a regression - added encryption unit test - added debug logging here and there Co-authored-by: Thomas von Dein <tom@vondein.org>
This commit is contained in:
20
app/db.go
20
app/db.go
@@ -20,6 +20,7 @@ import (
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"log/slog"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"regexp"
|
||||
@@ -84,6 +85,8 @@ func New(file string, bucket string, debug bool) (*DB, error) {
|
||||
}
|
||||
|
||||
func (db *DB) Open() error {
|
||||
slog.Debug("opening DB", "dbfile", db.Dbfile)
|
||||
|
||||
if _, err := os.Stat(filepath.Dir(db.Dbfile)); os.IsNotExist(err) {
|
||||
if err := os.MkdirAll(filepath.Dir(db.Dbfile), 0700); err != nil {
|
||||
return err
|
||||
@@ -128,11 +131,15 @@ func (db *DB) List(attr *DbAttr, fulltext bool) (DbEntries, error) {
|
||||
return nil
|
||||
}
|
||||
|
||||
slog.Debug("opened root bucket", "root", root)
|
||||
|
||||
bucket := root.Bucket([]byte("meta"))
|
||||
if bucket == nil {
|
||||
return nil
|
||||
}
|
||||
|
||||
slog.Debug("opened buckets", "root", root, "data", bucket)
|
||||
|
||||
databucket := root.Bucket([]byte("data"))
|
||||
if databucket == nil {
|
||||
return fmt.Errorf("failed to retrieve data sub bucket")
|
||||
@@ -215,6 +222,7 @@ func (db *DB) Set(attr *DbAttr) error {
|
||||
// check if the entry already exists and if yes, check if it has
|
||||
// any tags. if so, we initialize our update struct with these
|
||||
// tags unless it has new tags configured.
|
||||
// FIXME: use Get()
|
||||
err := db.DB.View(func(tx *bolt.Tx) error {
|
||||
root := tx.Bucket([]byte(db.Bucket))
|
||||
if root == nil {
|
||||
@@ -226,6 +234,8 @@ func (db *DB) Set(attr *DbAttr) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
slog.Debug("opened buckets", "root", root, "data", bucket)
|
||||
|
||||
pbentry := bucket.Get([]byte(entry.Key))
|
||||
if pbentry == nil {
|
||||
return nil
|
||||
@@ -267,6 +277,8 @@ func (db *DB) Set(attr *DbAttr) error {
|
||||
return fmt.Errorf("failed to create DB meta sub bucket: %w", err)
|
||||
}
|
||||
|
||||
slog.Debug("opened/created buckets", "root", root, "data", bucket)
|
||||
|
||||
// write meta data
|
||||
err = bucket.Put([]byte(entry.Key), []byte(pbentry))
|
||||
if err != nil {
|
||||
@@ -316,6 +328,8 @@ func (db *DB) Get(attr *DbAttr) (*DbEntry, error) {
|
||||
return nil
|
||||
}
|
||||
|
||||
slog.Debug("opened buckets", "root", root, "data", bucket)
|
||||
|
||||
// retrieve meta data
|
||||
pbentry := bucket.Get([]byte(attr.Key))
|
||||
if pbentry == nil {
|
||||
@@ -369,6 +383,8 @@ func (db *DB) Del(attr *DbAttr) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
slog.Debug("opened buckets", "data", bucket)
|
||||
|
||||
return bucket.Delete([]byte(attr.Key))
|
||||
})
|
||||
|
||||
@@ -421,6 +437,8 @@ func (db *DB) Import(attr *DbAttr) (string, error) {
|
||||
return fmt.Errorf("failed to create DB meta sub bucket: %w", err)
|
||||
}
|
||||
|
||||
slog.Debug("opened buckets", "root", root, "data", bucket)
|
||||
|
||||
for _, entry := range entries {
|
||||
pbentry, err := proto.Marshal(entry)
|
||||
if err != nil {
|
||||
@@ -528,6 +546,8 @@ func (db *DB) Getall(attr *DbAttr) (DbEntries, error) {
|
||||
return fmt.Errorf("failed to retrieve data sub bucket")
|
||||
}
|
||||
|
||||
slog.Debug("opened buckets", "root", root, "data", bucket)
|
||||
|
||||
// iterate over all db entries in meta sub bucket
|
||||
err := bucket.ForEach(func(key, pbentry []byte) error {
|
||||
var entry DbEntry
|
||||
|
||||
Reference in New Issue
Block a user