diff --git a/api/database.go b/api/database.go index 106a8d9..8f2c4e0 100644 --- a/api/database.go +++ b/api/database.go @@ -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 { diff --git a/config/main.go b/config/main.go index a37f8f6..df432c2 100644 --- a/config/main.go +++ b/config/main.go @@ -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") } diff --git a/database/database_test.go b/database/database_test.go index 3d59556..f2e2920 100644 --- a/database/database_test.go +++ b/database/database_test.go @@ -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) } diff --git a/database/dbmodule/db.go b/database/dbmodule/db.go index 13903c6..9a4c430 100644 --- a/database/dbmodule/db.go +++ b/database/dbmodule/db.go @@ -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 - databaseStructureRoot = dirStructureRoot +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 } diff --git a/database/dbmodule/maintenance.go b/database/dbmodule/maintenance.go index 8958299..6a1755c 100644 --- a/database/dbmodule/maintenance.go +++ b/database/dbmodule/maintenance.go @@ -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() } diff --git a/database/main.go b/database/main.go index d49962c..74c31a3 100644 --- a/database/main.go +++ b/database/main.go @@ -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 { - rootStructure = dirStructureRoot - } else { - rootStructure = utils.NewDirStructure(dirPath, 0755) - } +// 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 // ensure root and databases dirs databasesStructure = rootStructure.ChildDir(databasesSubDir, 0700) diff --git a/database/registry.go b/database/registry.go index b6d1984..22e6bab 100644 --- a/database/registry.go +++ b/database/registry.go @@ -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) } diff --git a/dataroot/root.go b/dataroot/root.go new file mode 100644 index 0000000..84c5275 --- /dev/null +++ b/dataroot/root.go @@ -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 +} diff --git a/log/input.go b/log/input.go index e8b6579..60f4af6 100644 --- a/log/input.go +++ b/log/input.go @@ -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) diff --git a/log/logging.go b/log/logging.go index 2d605c8..d28039f 100644 --- a/log/logging.go +++ b/log/logging.go @@ -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() { - close(shutdownSignal) + if shutdownFlag.SetToIf(false, true) { + close(shutdownSignal) + } shutdownWaitGroup.Wait() } diff --git a/log/trace.go b/log/trace.go index a345f5d..f90b306 100644 --- a/log/trace.go +++ b/log/trace.go @@ -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()