mirror of
https://github.com/safing/portbase
synced 2025-09-01 01:59:48 +00:00
Update packages to new modules structure and other minor fixes
This commit is contained in:
parent
3f17d0e7a9
commit
1f77046877
11 changed files with 75 additions and 52 deletions
|
@ -243,8 +243,8 @@ func (api *DatabaseAPI) handleGet(opID []byte, key string) {
|
||||||
if err == nil {
|
if err == nil {
|
||||||
data, err = r.Marshal(r, record.JSON)
|
data, err = r.Marshal(r, record.JSON)
|
||||||
}
|
}
|
||||||
if err == nil {
|
if err != nil {
|
||||||
api.send(opID, dbMsgTypeError, err.Error(), nil) //nolint:nilness // FIXME: possibly false positive (golangci-lint govet/nilness)
|
api.send(opID, dbMsgTypeError, err.Error(), nil)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
api.send(opID, dbMsgTypeOk, r.Key(), data)
|
api.send(opID, dbMsgTypeOk, r.Key(), data)
|
||||||
|
@ -435,13 +435,13 @@ func (api *DatabaseAPI) handlePut(opID []byte, key string, data []byte, create b
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
// FIXME: remove transition code
|
// TODO - staged for deletion: remove transition code
|
||||||
if data[0] != record.JSON {
|
// if data[0] != record.JSON {
|
||||||
typedData := make([]byte, len(data)+1)
|
// typedData := make([]byte, len(data)+1)
|
||||||
typedData[0] = record.JSON
|
// typedData[0] = record.JSON
|
||||||
copy(typedData[1:], data)
|
// copy(typedData[1:], data)
|
||||||
data = typedData
|
// data = typedData
|
||||||
}
|
// }
|
||||||
|
|
||||||
r, err := record.NewWrapper(key, nil, data[0], data[1:])
|
r, err := record.NewWrapper(key, nil, data[0], data[1:])
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|
|
@ -5,9 +5,9 @@ import (
|
||||||
"os"
|
"os"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
|
|
||||||
|
"github.com/safing/portbase/dataroot"
|
||||||
"github.com/safing/portbase/modules"
|
"github.com/safing/portbase/modules"
|
||||||
"github.com/safing/portbase/utils"
|
"github.com/safing/portbase/utils"
|
||||||
"github.com/safing/portmaster/core/structure"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
|
@ -27,12 +27,12 @@ func SetDataRoot(root *utils.DirStructure) {
|
||||||
}
|
}
|
||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
module = modules.Register("config", prep, start, nil, "base", "database")
|
module = modules.Register("config", prep, start, nil, "database")
|
||||||
module.RegisterEvent(configChangeEvent)
|
module.RegisterEvent(configChangeEvent)
|
||||||
}
|
}
|
||||||
|
|
||||||
func prep() error {
|
func prep() error {
|
||||||
SetDataRoot(structure.Root())
|
SetDataRoot(dataroot.Root())
|
||||||
if dataRoot == nil {
|
if dataRoot == nil {
|
||||||
return errors.New("data root is not set")
|
return errors.New("data root is not set")
|
||||||
}
|
}
|
||||||
|
|
|
@ -128,12 +128,12 @@ func TestDatabaseSystem(t *testing.T) {
|
||||||
os.Exit(1)
|
os.Exit(1)
|
||||||
}()
|
}()
|
||||||
|
|
||||||
testDir, err := ioutil.TempDir("", "testing-")
|
testDir, err := ioutil.TempDir("", "portbase-database-testing-")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
err = Initialize(testDir, nil)
|
err = InitializeWithPath(testDir)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
}
|
}
|
||||||
|
|
|
@ -4,41 +4,44 @@ import (
|
||||||
"errors"
|
"errors"
|
||||||
|
|
||||||
"github.com/safing/portbase/database"
|
"github.com/safing/portbase/database"
|
||||||
|
"github.com/safing/portbase/dataroot"
|
||||||
"github.com/safing/portbase/modules"
|
"github.com/safing/portbase/modules"
|
||||||
"github.com/safing/portbase/utils"
|
"github.com/safing/portbase/utils"
|
||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
databasePath string
|
|
||||||
databaseStructureRoot *utils.DirStructure
|
databaseStructureRoot *utils.DirStructure
|
||||||
|
|
||||||
module *modules.Module
|
module *modules.Module
|
||||||
)
|
)
|
||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
module = modules.Register("database", prep, start, stop, "base")
|
module = modules.Register("database", prep, start, stop)
|
||||||
}
|
}
|
||||||
|
|
||||||
// SetDatabaseLocation sets the location of the database for initialization. Supply either a path or dir structure.
|
// SetDatabaseLocation sets the location of the database for initialization. Supply either a path or dir structure.
|
||||||
func SetDatabaseLocation(dirPath string, dirStructureRoot *utils.DirStructure) {
|
func SetDatabaseLocation(dirStructureRoot *utils.DirStructure) {
|
||||||
databasePath = dirPath
|
if databaseStructureRoot == nil {
|
||||||
databaseStructureRoot = dirStructureRoot
|
databaseStructureRoot = dirStructureRoot
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func prep() error {
|
func prep() error {
|
||||||
if databasePath == "" && databaseStructureRoot == nil {
|
SetDatabaseLocation(dataroot.Root())
|
||||||
return errors.New("no database location specified")
|
if databaseStructureRoot == nil {
|
||||||
|
return errors.New("database location not specified")
|
||||||
}
|
}
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func start() error {
|
func start() error {
|
||||||
err := database.Initialize(databasePath, databaseStructureRoot)
|
err := database.Initialize(databaseStructureRoot)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
registerMaintenanceTasks()
|
startMaintenanceTasks()
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -5,33 +5,23 @@ import (
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/safing/portbase/database"
|
"github.com/safing/portbase/database"
|
||||||
"github.com/safing/portbase/log"
|
|
||||||
"github.com/safing/portbase/modules"
|
"github.com/safing/portbase/modules"
|
||||||
)
|
)
|
||||||
|
|
||||||
func registerMaintenanceTasks() {
|
func startMaintenanceTasks() {
|
||||||
module.NewTask("basic maintenance", maintainBasic).Repeat(10 * time.Minute).MaxDelay(10 * time.Minute)
|
module.NewTask("basic maintenance", maintainBasic).Repeat(10 * time.Minute).MaxDelay(10 * time.Minute)
|
||||||
module.NewTask("thorough maintenance", maintainThorough).Repeat(1 * time.Hour).MaxDelay(1 * time.Hour)
|
module.NewTask("thorough maintenance", maintainThorough).Repeat(1 * time.Hour).MaxDelay(1 * time.Hour)
|
||||||
module.NewTask("record maintenance", maintainRecords).Repeat(1 * time.Hour).MaxDelay(1 * time.Hour)
|
module.NewTask("record maintenance", maintainRecords).Repeat(1 * time.Hour).MaxDelay(1 * time.Hour)
|
||||||
}
|
}
|
||||||
|
|
||||||
func maintainBasic(ctx context.Context, task *modules.Task) {
|
func maintainBasic(ctx context.Context, task *modules.Task) error {
|
||||||
err := database.Maintain()
|
return database.Maintain()
|
||||||
if err != nil {
|
|
||||||
log.Errorf("database: maintenance error: %s", err)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func maintainThorough(ctx context.Context, task *modules.Task) {
|
func maintainThorough(ctx context.Context, task *modules.Task) error {
|
||||||
err := database.MaintainThorough()
|
return database.MaintainThorough()
|
||||||
if err != nil {
|
|
||||||
log.Errorf("database: thorough maintenance error: %s", err)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func maintainRecords(ctx context.Context, task *modules.Task) {
|
func maintainRecords(ctx context.Context, task *modules.Task) error {
|
||||||
err := database.MaintainRecordStates()
|
return database.MaintainRecordStates()
|
||||||
if err != nil {
|
|
||||||
log.Errorf("database: record states maintenance error: %s", err)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -23,15 +23,15 @@ var (
|
||||||
databasesStructure *utils.DirStructure
|
databasesStructure *utils.DirStructure
|
||||||
)
|
)
|
||||||
|
|
||||||
// Initialize initializes the database at the specified location. Supply either a path or dir structure.
|
// InitializeWithPath initializes the database at the specified location using a path.
|
||||||
func Initialize(dirPath string, dirStructureRoot *utils.DirStructure) error {
|
func InitializeWithPath(dirPath string) error {
|
||||||
if initialized.SetToIf(false, true) {
|
return Initialize(utils.NewDirStructure(dirPath, 0755))
|
||||||
|
}
|
||||||
|
|
||||||
if dirStructureRoot != nil {
|
// Initialize initializes the database at the specified location using a dir structure.
|
||||||
rootStructure = dirStructureRoot
|
func Initialize(dirStructureRoot *utils.DirStructure) error {
|
||||||
} else {
|
if initialized.SetToIf(false, true) {
|
||||||
rootStructure = utils.NewDirStructure(dirPath, 0755)
|
rootStructure = dirStructureRoot
|
||||||
}
|
|
||||||
|
|
||||||
// ensure root and databases dirs
|
// ensure root and databases dirs
|
||||||
databasesStructure = rootStructure.ChildDir(databasesSubDir, 0700)
|
databasesStructure = rootStructure.ChildDir(databasesSubDir, 0700)
|
||||||
|
|
|
@ -139,7 +139,7 @@ func saveRegistry(lock bool) error {
|
||||||
}
|
}
|
||||||
|
|
||||||
// write file
|
// write file
|
||||||
// FIXME: write atomically (best effort)
|
// TODO: write atomically (best effort)
|
||||||
filePath := path.Join(rootStructure.Path, registryFileName)
|
filePath := path.Join(rootStructure.Path, registryFileName)
|
||||||
return ioutil.WriteFile(filePath, data, 0600)
|
return ioutil.WriteFile(filePath, data, 0600)
|
||||||
}
|
}
|
||||||
|
|
27
dataroot/root.go
Normal file
27
dataroot/root.go
Normal file
|
@ -0,0 +1,27 @@
|
||||||
|
package dataroot
|
||||||
|
|
||||||
|
import (
|
||||||
|
"errors"
|
||||||
|
"os"
|
||||||
|
|
||||||
|
"github.com/safing/portbase/utils"
|
||||||
|
)
|
||||||
|
|
||||||
|
var (
|
||||||
|
root *utils.DirStructure
|
||||||
|
)
|
||||||
|
|
||||||
|
// Initialize initializes the data root directory
|
||||||
|
func Initialize(rootDir string, perm os.FileMode) error {
|
||||||
|
if root != nil {
|
||||||
|
return errors.New("already initialized")
|
||||||
|
}
|
||||||
|
|
||||||
|
root = utils.NewDirStructure(rootDir, perm)
|
||||||
|
return root.Ensure()
|
||||||
|
}
|
||||||
|
|
||||||
|
// Root returns the data root directory.
|
||||||
|
func Root() *utils.DirStructure {
|
||||||
|
return root
|
||||||
|
}
|
|
@ -12,7 +12,7 @@ func log(level Severity, msg string, tracer *ContextTracer) {
|
||||||
|
|
||||||
if !started.IsSet() {
|
if !started.IsSet() {
|
||||||
// a bit resource intense, but keeps logs before logging started.
|
// a bit resource intense, but keeps logs before logging started.
|
||||||
// FIXME: create option to disable logging
|
// TODO: create option to disable logging
|
||||||
go func() {
|
go func() {
|
||||||
<-startedSignal
|
<-startedSignal
|
||||||
log(level, msg, tracer)
|
log(level, msg, tracer)
|
||||||
|
|
|
@ -82,6 +82,7 @@ var (
|
||||||
logsWaiting = make(chan struct{}, 4)
|
logsWaiting = make(chan struct{}, 4)
|
||||||
logsWaitingFlag = abool.NewBool(false)
|
logsWaitingFlag = abool.NewBool(false)
|
||||||
|
|
||||||
|
shutdownFlag = abool.NewBool(false)
|
||||||
shutdownSignal = make(chan struct{})
|
shutdownSignal = make(chan struct{})
|
||||||
shutdownWaitGroup sync.WaitGroup
|
shutdownWaitGroup sync.WaitGroup
|
||||||
|
|
||||||
|
@ -179,6 +180,8 @@ func Start() (err error) {
|
||||||
|
|
||||||
// Shutdown writes remaining log lines and then stops the log system.
|
// Shutdown writes remaining log lines and then stops the log system.
|
||||||
func Shutdown() {
|
func Shutdown() {
|
||||||
close(shutdownSignal)
|
if shutdownFlag.SetToIf(false, true) {
|
||||||
|
close(shutdownSignal)
|
||||||
|
}
|
||||||
shutdownWaitGroup.Wait()
|
shutdownWaitGroup.Wait()
|
||||||
}
|
}
|
||||||
|
|
|
@ -89,7 +89,7 @@ func (tracer *ContextTracer) Submit() {
|
||||||
|
|
||||||
if !started.IsSet() {
|
if !started.IsSet() {
|
||||||
// a bit resource intense, but keeps logs before logging started.
|
// a bit resource intense, but keeps logs before logging started.
|
||||||
// FIXME: create option to disable logging
|
// TODO: create option to disable logging
|
||||||
go func() {
|
go func() {
|
||||||
<-startedSignal
|
<-startedSignal
|
||||||
tracer.Submit()
|
tracer.Submit()
|
||||||
|
|
Loading…
Add table
Reference in a new issue