Update packages to new modules structure and other minor fixes

This commit is contained in:
Daniel 2020-02-18 15:44:18 +01:00
parent 3f17d0e7a9
commit 1f77046877
11 changed files with 75 additions and 52 deletions

View file

@ -243,8 +243,8 @@ func (api *DatabaseAPI) handleGet(opID []byte, key string) {
if err == nil {
data, err = r.Marshal(r, record.JSON)
}
if err == nil {
api.send(opID, dbMsgTypeError, err.Error(), nil) //nolint:nilness // FIXME: possibly false positive (golangci-lint govet/nilness)
if err != nil {
api.send(opID, dbMsgTypeError, err.Error(), nil)
return
}
api.send(opID, dbMsgTypeOk, r.Key(), data)
@ -435,13 +435,13 @@ func (api *DatabaseAPI) handlePut(opID []byte, key string, data []byte, create b
return
}
// FIXME: remove transition code
if data[0] != record.JSON {
typedData := make([]byte, len(data)+1)
typedData[0] = record.JSON
copy(typedData[1:], data)
data = typedData
}
// TODO - staged for deletion: remove transition code
// if data[0] != record.JSON {
// typedData := make([]byte, len(data)+1)
// typedData[0] = record.JSON
// copy(typedData[1:], data)
// data = typedData
// }
r, err := record.NewWrapper(key, nil, data[0], data[1:])
if err != nil {

View file

@ -5,9 +5,9 @@ import (
"os"
"path/filepath"
"github.com/safing/portbase/dataroot"
"github.com/safing/portbase/modules"
"github.com/safing/portbase/utils"
"github.com/safing/portmaster/core/structure"
)
const (
@ -27,12 +27,12 @@ func SetDataRoot(root *utils.DirStructure) {
}
func init() {
module = modules.Register("config", prep, start, nil, "base", "database")
module = modules.Register("config", prep, start, nil, "database")
module.RegisterEvent(configChangeEvent)
}
func prep() error {
SetDataRoot(structure.Root())
SetDataRoot(dataroot.Root())
if dataRoot == nil {
return errors.New("data root is not set")
}

View file

@ -128,12 +128,12 @@ func TestDatabaseSystem(t *testing.T) {
os.Exit(1)
}()
testDir, err := ioutil.TempDir("", "testing-")
testDir, err := ioutil.TempDir("", "portbase-database-testing-")
if err != nil {
t.Fatal(err)
}
err = Initialize(testDir, nil)
err = InitializeWithPath(testDir)
if err != nil {
t.Fatal(err)
}

View file

@ -4,41 +4,44 @@ import (
"errors"
"github.com/safing/portbase/database"
"github.com/safing/portbase/dataroot"
"github.com/safing/portbase/modules"
"github.com/safing/portbase/utils"
)
var (
databasePath string
databaseStructureRoot *utils.DirStructure
module *modules.Module
)
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.
func SetDatabaseLocation(dirPath string, dirStructureRoot *utils.DirStructure) {
databasePath = dirPath
func SetDatabaseLocation(dirStructureRoot *utils.DirStructure) {
if databaseStructureRoot == nil {
databaseStructureRoot = dirStructureRoot
}
}
func prep() error {
if databasePath == "" && databaseStructureRoot == nil {
return errors.New("no database location specified")
SetDatabaseLocation(dataroot.Root())
if databaseStructureRoot == nil {
return errors.New("database location not specified")
}
return nil
}
func start() error {
err := database.Initialize(databasePath, databaseStructureRoot)
err := database.Initialize(databaseStructureRoot)
if err != nil {
return err
}
registerMaintenanceTasks()
startMaintenanceTasks()
return nil
}

View file

@ -5,33 +5,23 @@ import (
"time"
"github.com/safing/portbase/database"
"github.com/safing/portbase/log"
"github.com/safing/portbase/modules"
)
func registerMaintenanceTasks() {
func startMaintenanceTasks() {
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("record maintenance", maintainRecords).Repeat(1 * time.Hour).MaxDelay(1 * time.Hour)
}
func maintainBasic(ctx context.Context, task *modules.Task) {
err := database.Maintain()
if err != nil {
log.Errorf("database: maintenance error: %s", err)
}
func maintainBasic(ctx context.Context, task *modules.Task) error {
return database.Maintain()
}
func maintainThorough(ctx context.Context, task *modules.Task) {
err := database.MaintainThorough()
if err != nil {
log.Errorf("database: thorough maintenance error: %s", err)
}
func maintainThorough(ctx context.Context, task *modules.Task) error {
return database.MaintainThorough()
}
func maintainRecords(ctx context.Context, task *modules.Task) {
err := database.MaintainRecordStates()
if err != nil {
log.Errorf("database: record states maintenance error: %s", err)
}
func maintainRecords(ctx context.Context, task *modules.Task) error {
return database.MaintainRecordStates()
}

View file

@ -23,15 +23,15 @@ var (
databasesStructure *utils.DirStructure
)
// Initialize initializes the database at the specified location. Supply either a path or dir structure.
func Initialize(dirPath string, dirStructureRoot *utils.DirStructure) error {
if initialized.SetToIf(false, true) {
// InitializeWithPath initializes the database at the specified location using a path.
func InitializeWithPath(dirPath string) error {
return Initialize(utils.NewDirStructure(dirPath, 0755))
}
if dirStructureRoot != nil {
// Initialize initializes the database at the specified location using a dir structure.
func Initialize(dirStructureRoot *utils.DirStructure) error {
if initialized.SetToIf(false, true) {
rootStructure = dirStructureRoot
} else {
rootStructure = utils.NewDirStructure(dirPath, 0755)
}
// ensure root and databases dirs
databasesStructure = rootStructure.ChildDir(databasesSubDir, 0700)

View file

@ -139,7 +139,7 @@ func saveRegistry(lock bool) error {
}
// write file
// FIXME: write atomically (best effort)
// TODO: write atomically (best effort)
filePath := path.Join(rootStructure.Path, registryFileName)
return ioutil.WriteFile(filePath, data, 0600)
}

27
dataroot/root.go Normal file
View 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
}

View file

@ -12,7 +12,7 @@ func log(level Severity, msg string, tracer *ContextTracer) {
if !started.IsSet() {
// a bit resource intense, but keeps logs before logging started.
// FIXME: create option to disable logging
// TODO: create option to disable logging
go func() {
<-startedSignal
log(level, msg, tracer)

View file

@ -82,6 +82,7 @@ var (
logsWaiting = make(chan struct{}, 4)
logsWaitingFlag = abool.NewBool(false)
shutdownFlag = abool.NewBool(false)
shutdownSignal = make(chan struct{})
shutdownWaitGroup sync.WaitGroup
@ -179,6 +180,8 @@ func Start() (err error) {
// Shutdown writes remaining log lines and then stops the log system.
func Shutdown() {
if shutdownFlag.SetToIf(false, true) {
close(shutdownSignal)
}
shutdownWaitGroup.Wait()
}

View file

@ -89,7 +89,7 @@ func (tracer *ContextTracer) Submit() {
if !started.IsSet() {
// a bit resource intense, but keeps logs before logging started.
// FIXME: create option to disable logging
// TODO: create option to disable logging
go func() {
<-startedSignal
tracer.Submit()