Replace dataroot module with BinDir and DataDir on instance, adapt modules

This commit is contained in:
Daniel
2024-11-06 10:48:02 +01:00
parent 0f3f3c360f
commit 7bc1c3b764
39 changed files with 819 additions and 482 deletions

View File

@@ -26,7 +26,7 @@ func TestMain(m *testing.M) {
panic(err)
}
err = InitializeWithPath(testDir)
err = Initialize(testDir)
if err != nil {
panic(err)
}

View File

@@ -2,11 +2,10 @@ package dbmodule
import (
"errors"
"path/filepath"
"sync/atomic"
"github.com/safing/portmaster/base/database"
"github.com/safing/portmaster/base/dataroot"
"github.com/safing/portmaster/base/utils"
"github.com/safing/portmaster/service/mgr"
)
@@ -27,18 +26,18 @@ func (dbm *DBModule) Stop() error {
return stop()
}
var databaseStructureRoot *utils.DirStructure
var databasesRootDir string
// SetDatabaseLocation sets the location of the database for initialization. Supply either a path or dir structure.
func SetDatabaseLocation(dirStructureRoot *utils.DirStructure) {
if databaseStructureRoot == nil {
databaseStructureRoot = dirStructureRoot
func SetDatabaseLocation(dir string) {
if databasesRootDir == "" {
databasesRootDir = dir
}
}
func prep() error {
SetDatabaseLocation(dataroot.Root())
if databaseStructureRoot == nil {
SetDatabaseLocation(filepath.Join(module.instance.DataDir(), "databases"))
if databasesRootDir == "" {
return errors.New("database location not specified")
}
@@ -64,16 +63,16 @@ func New(instance instance) (*DBModule, error) {
return nil, errors.New("only one instance allowed")
}
if err := prep(); err != nil {
return nil, err
}
m := mgr.New("DBModule")
module = &DBModule{
mgr: m,
instance: instance,
}
if err := prep(); err != nil {
return nil, err
}
err := database.Initialize(databaseStructureRoot)
err := database.Initialize(databasesRootDir)
if err != nil {
return nil, err
}
@@ -81,4 +80,6 @@ func New(instance instance) (*DBModule, error) {
return module, nil
}
type instance interface{}
type instance interface {
DataDir() string
}

View File

@@ -3,14 +3,10 @@ package database
import (
"errors"
"fmt"
"os"
"path/filepath"
"github.com/tevino/abool"
"github.com/safing/portmaster/base/utils"
)
const (
databasesSubDir = "databases"
)
var (
@@ -19,25 +15,18 @@ var (
shuttingDown = abool.NewBool(false)
shutdownSignal = make(chan struct{})
rootStructure *utils.DirStructure
databasesStructure *utils.DirStructure
rootDir string
)
// InitializeWithPath initializes the database at the specified location using a path.
func InitializeWithPath(dirPath string) error {
return Initialize(utils.NewDirStructure(dirPath, 0o0755))
}
// Initialize initializes the database at the specified location using a dir structure.
func Initialize(dirStructureRoot *utils.DirStructure) error {
// Initialize initializes the database at the specified location.
func Initialize(databasesRootDir string) error {
if initialized.SetToIf(false, true) {
rootStructure = dirStructureRoot
rootDir = databasesRootDir
// ensure root and databases dirs
databasesStructure = rootStructure.ChildDir(databasesSubDir, 0o0700)
err := databasesStructure.Ensure()
// Ensure database root dir exists.
err := os.MkdirAll(rootDir, 0o0700)
if err != nil {
return fmt.Errorf("could not create/open database directory (%s): %w", rootStructure.Path, err)
return fmt.Errorf("could not create/open database directory (%s): %w", rootDir, err)
}
return nil
@@ -67,11 +56,12 @@ func Shutdown() (err error) {
// getLocation returns the storage location for the given name and type.
func getLocation(name, storageType string) (string, error) {
location := databasesStructure.ChildDir(name, 0o0700).ChildDir(storageType, 0o0700)
// check location
err := location.Ensure()
location := filepath.Join(rootDir, name, storageType)
// Make sure location exists.
err := os.MkdirAll(location, 0o0700)
if err != nil {
return "", fmt.Errorf(`failed to create/check database dir "%s": %w`, location.Path, err)
return "", fmt.Errorf("failed to create/check database dir %q: %w", location, err)
}
return location.Path, nil
return location, nil
}