Replace dataroot module with BinDir and DataDir on instance, adapt modules
This commit is contained in:
@@ -6,30 +6,50 @@ import (
|
||||
"testing"
|
||||
)
|
||||
|
||||
type testInstance struct{}
|
||||
type testInstance struct {
|
||||
dataDir string
|
||||
}
|
||||
|
||||
var _ instance = testInstance{}
|
||||
|
||||
func (stub testInstance) DataDir() string {
|
||||
return stub.dataDir
|
||||
}
|
||||
|
||||
func (stub testInstance) SetCmdLineOperation(f func() error) {}
|
||||
|
||||
func runTest(m *testing.M) error {
|
||||
ds, err := InitializeUnitTestDataroot("test-config")
|
||||
func newTestInstance(testName string) (*testInstance, error) {
|
||||
testDir, err := os.MkdirTemp("", fmt.Sprintf("portmaster-%s", testName))
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to initialize dataroot: %w", err)
|
||||
}
|
||||
defer func() { _ = os.RemoveAll(ds) }()
|
||||
module, err = New(&testInstance{})
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to initialize module: %w", err)
|
||||
return nil, fmt.Errorf("failed to make tmp dir: %w", err)
|
||||
}
|
||||
|
||||
m.Run()
|
||||
return nil
|
||||
return &testInstance{
|
||||
dataDir: testDir,
|
||||
}, nil
|
||||
}
|
||||
|
||||
func TestMain(m *testing.M) {
|
||||
if err := runTest(m); err != nil {
|
||||
fmt.Printf("%s\n", err)
|
||||
os.Exit(1)
|
||||
func TestConfigPersistence(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
instance, err := newTestInstance("test-config")
|
||||
if err != nil {
|
||||
t.Fatalf("failed to create test instance: %s", err)
|
||||
}
|
||||
defer func() { _ = os.RemoveAll(instance.DataDir()) }()
|
||||
|
||||
module, err = New(instance)
|
||||
if err != nil {
|
||||
t.Fatalf("failed to initialize module: %s", err)
|
||||
}
|
||||
|
||||
err = SaveConfig()
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
err = loadConfig(true)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -10,8 +10,6 @@ import (
|
||||
"path/filepath"
|
||||
"sort"
|
||||
|
||||
"github.com/safing/portmaster/base/dataroot"
|
||||
"github.com/safing/portmaster/base/utils"
|
||||
"github.com/safing/portmaster/base/utils/debug"
|
||||
"github.com/safing/portmaster/service/mgr"
|
||||
)
|
||||
@@ -19,29 +17,13 @@ import (
|
||||
// ChangeEvent is the name of the config change event.
|
||||
const ChangeEvent = "config change"
|
||||
|
||||
var (
|
||||
dataRoot *utils.DirStructure
|
||||
|
||||
exportConfig bool
|
||||
)
|
||||
|
||||
// SetDataRoot sets the data root from which the updates module derives its paths.
|
||||
func SetDataRoot(root *utils.DirStructure) {
|
||||
if dataRoot == nil {
|
||||
dataRoot = root
|
||||
}
|
||||
}
|
||||
var exportConfig bool
|
||||
|
||||
func init() {
|
||||
flag.BoolVar(&exportConfig, "export-config-options", false, "export configuration registry and exit")
|
||||
}
|
||||
|
||||
func prep() error {
|
||||
SetDataRoot(dataroot.Root())
|
||||
if dataRoot == nil {
|
||||
return errors.New("data root is not set")
|
||||
}
|
||||
|
||||
if exportConfig {
|
||||
module.instance.SetCmdLineOperation(exportConfigCmd)
|
||||
return mgr.ErrExecuteCmdLineOp
|
||||
@@ -51,7 +33,7 @@ func prep() error {
|
||||
}
|
||||
|
||||
func start() error {
|
||||
configFilePath = filepath.Join(dataRoot.Path, "config.json")
|
||||
configFilePath = filepath.Join(module.instance.DataDir(), "config.json")
|
||||
|
||||
// Load log level from log package after it started.
|
||||
err := loadLogLevel()
|
||||
@@ -136,20 +118,3 @@ func GetActiveConfigValues() map[string]interface{} {
|
||||
|
||||
return values
|
||||
}
|
||||
|
||||
// InitializeUnitTestDataroot initializes a new random tmp directory for running tests.
|
||||
func InitializeUnitTestDataroot(testName string) (string, error) {
|
||||
basePath, err := os.MkdirTemp("", fmt.Sprintf("portmaster-%s", testName))
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("failed to make tmp dir: %w", err)
|
||||
}
|
||||
|
||||
ds := utils.NewDirStructure(basePath, 0o0755)
|
||||
SetDataRoot(ds)
|
||||
err = dataroot.Initialize(basePath, 0o0755)
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("failed to initialize dataroot: %w", err)
|
||||
}
|
||||
|
||||
return basePath, nil
|
||||
}
|
||||
|
||||
@@ -56,5 +56,6 @@ func New(instance instance) (*Config, error) {
|
||||
}
|
||||
|
||||
type instance interface {
|
||||
DataDir() string
|
||||
SetCmdLineOperation(f func() error)
|
||||
}
|
||||
|
||||
@@ -26,7 +26,7 @@ func TestMain(m *testing.M) {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
err = InitializeWithPath(testDir)
|
||||
err = Initialize(testDir)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
@@ -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
|
||||
}
|
||||
|
||||
@@ -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
|
||||
}
|
||||
|
||||
@@ -1,25 +0,0 @@
|
||||
package dataroot
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"os"
|
||||
|
||||
"github.com/safing/portmaster/base/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
|
||||
}
|
||||
@@ -10,7 +10,6 @@ import (
|
||||
"github.com/shirou/gopsutil/mem"
|
||||
|
||||
"github.com/safing/portmaster/base/api"
|
||||
"github.com/safing/portmaster/base/dataroot"
|
||||
"github.com/safing/portmaster/base/log"
|
||||
)
|
||||
|
||||
@@ -209,18 +208,9 @@ func getDiskStat() *disk.UsageStat {
|
||||
return diskStat
|
||||
}
|
||||
|
||||
// Check if we have a data root.
|
||||
dataRoot := dataroot.Root()
|
||||
if dataRoot == nil {
|
||||
log.Warning("metrics: cannot get disk stats without data root")
|
||||
diskStat = nil
|
||||
diskStatExpires = time.Now().Add(hostStatTTL)
|
||||
return diskStat
|
||||
}
|
||||
|
||||
// Refresh.
|
||||
var err error
|
||||
diskStat, err = disk.Usage(dataRoot.Path)
|
||||
diskStat, err = disk.Usage(module.instance.DataDir())
|
||||
if err != nil {
|
||||
log.Warningf("metrics: failed to get load avg: %s", err)
|
||||
diskStat = nil
|
||||
|
||||
@@ -213,4 +213,6 @@ func New(instance instance) (*Metrics, error) {
|
||||
return module, nil
|
||||
}
|
||||
|
||||
type instance interface{}
|
||||
type instance interface {
|
||||
DataDir() string
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user