mirror of
https://github.com/safing/portbase
synced 2025-09-02 10:40:39 +00:00
Switch to new data root / core module structure
This commit is contained in:
parent
c4b9c73f41
commit
2ac64aedff
5 changed files with 55 additions and 53 deletions
|
@ -13,7 +13,7 @@ var (
|
||||||
)
|
)
|
||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
modules.Register("api", prep, start, nil, "database")
|
modules.Register("api", prep, start, nil, "core")
|
||||||
}
|
}
|
||||||
|
|
||||||
func prep() error {
|
func prep() error {
|
||||||
|
|
|
@ -1,23 +1,40 @@
|
||||||
package config
|
package config
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"errors"
|
||||||
"os"
|
"os"
|
||||||
"path"
|
"path/filepath"
|
||||||
|
|
||||||
"github.com/safing/portbase/database"
|
|
||||||
"github.com/safing/portbase/modules"
|
"github.com/safing/portbase/modules"
|
||||||
|
"github.com/safing/portbase/utils"
|
||||||
|
"github.com/safing/portmaster/core/structure"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
var (
|
||||||
|
dataRoot *utils.DirStructure
|
||||||
|
)
|
||||||
|
|
||||||
|
// SetDataRoot sets the data root from which the updates module derives its paths.
|
||||||
|
func SetDataRoot(root *utils.DirStructure) {
|
||||||
|
if dataRoot == nil {
|
||||||
|
dataRoot = root
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
modules.Register("config", prep, start, nil, "database")
|
modules.Register("config", prep, start, nil, "core")
|
||||||
}
|
}
|
||||||
|
|
||||||
func prep() error {
|
func prep() error {
|
||||||
|
SetDataRoot(structure.Root())
|
||||||
|
if dataRoot == nil {
|
||||||
|
return errors.New("data root is not set")
|
||||||
|
}
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func start() error {
|
func start() error {
|
||||||
configFilePath = path.Join(database.GetDatabaseRoot(), "config.json")
|
configFilePath = filepath.Join(dataRoot.Path, "config.json")
|
||||||
|
|
||||||
err := registerAsDatabase()
|
err := registerAsDatabase()
|
||||||
if err != nil && !os.IsNotExist(err) {
|
if err != nil && !os.IsNotExist(err) {
|
||||||
|
|
|
@ -1,33 +1 @@
|
||||||
package database
|
package database
|
||||||
|
|
||||||
import (
|
|
||||||
"fmt"
|
|
||||||
"path/filepath"
|
|
||||||
|
|
||||||
"github.com/safing/portbase/utils"
|
|
||||||
)
|
|
||||||
|
|
||||||
const (
|
|
||||||
databasesSubDir = "databases"
|
|
||||||
)
|
|
||||||
|
|
||||||
var (
|
|
||||||
rootDir string
|
|
||||||
)
|
|
||||||
|
|
||||||
// GetDatabaseRoot returns the root directory of the database.
|
|
||||||
func GetDatabaseRoot() string {
|
|
||||||
return rootDir
|
|
||||||
}
|
|
||||||
|
|
||||||
// getLocation returns the storage location for the given name and type.
|
|
||||||
func getLocation(name, storageType string) (string, error) {
|
|
||||||
location := filepath.Join(rootDir, databasesSubDir, name, storageType)
|
|
||||||
|
|
||||||
// check location
|
|
||||||
err := utils.EnsureDirectory(location, 0700)
|
|
||||||
if err != nil {
|
|
||||||
return "", fmt.Errorf("location (%s) invalid: %s", location, err)
|
|
||||||
}
|
|
||||||
return location, nil
|
|
||||||
}
|
|
||||||
|
|
|
@ -9,34 +9,40 @@ import (
|
||||||
"github.com/tevino/abool"
|
"github.com/tevino/abool"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
const (
|
||||||
|
databasesSubDir = "databases"
|
||||||
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
initialized = abool.NewBool(false)
|
initialized = abool.NewBool(false)
|
||||||
|
|
||||||
shuttingDown = abool.NewBool(false)
|
shuttingDown = abool.NewBool(false)
|
||||||
shutdownSignal = make(chan struct{})
|
shutdownSignal = make(chan struct{})
|
||||||
|
|
||||||
|
rootStructure *utils.DirStructure
|
||||||
|
databasesStructure *utils.DirStructure
|
||||||
)
|
)
|
||||||
|
|
||||||
// SetLocation sets the location of the database. This is separate from the initialization to provide the location to other modules earlier.
|
// Initialize initialized the database at the specified location. Supply either a path or dir structure.
|
||||||
func SetLocation(location string) (ok bool) {
|
func Initialize(dirPath string, dirStructureRoot *utils.DirStructure) error {
|
||||||
if !initialized.IsSet() && rootDir == "" {
|
|
||||||
rootDir = location
|
|
||||||
return true
|
|
||||||
}
|
|
||||||
return false
|
|
||||||
}
|
|
||||||
|
|
||||||
// Initialize initialized the database
|
|
||||||
func Initialize() error {
|
|
||||||
if initialized.SetToIf(false, true) {
|
if initialized.SetToIf(false, true) {
|
||||||
|
|
||||||
err := utils.EnsureDirectory(rootDir, 0755)
|
if dirStructureRoot != nil {
|
||||||
|
rootStructure = dirStructureRoot
|
||||||
|
} else {
|
||||||
|
rootStructure = utils.NewDirStructure(dirPath, 0755)
|
||||||
|
}
|
||||||
|
|
||||||
|
// ensure root and databases dirs
|
||||||
|
databasesStructure = rootStructure.ChildDir(databasesSubDir, 0700)
|
||||||
|
err := databasesStructure.Ensure()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("could not create/open database directory (%s): %s", rootDir, err)
|
return fmt.Errorf("could not create/open database directory (%s): %s", rootStructure.Path, err)
|
||||||
}
|
}
|
||||||
|
|
||||||
err = loadRegistry()
|
err = loadRegistry()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("could not load database registry (%s): %s", filepath.Join(rootDir, registryFileName), err)
|
return fmt.Errorf("could not load database registry (%s): %s", filepath.Join(rootStructure.Path, registryFileName), err)
|
||||||
}
|
}
|
||||||
|
|
||||||
// start registry writer
|
// start registry writer
|
||||||
|
@ -66,3 +72,14 @@ func Shutdown() (err error) {
|
||||||
}
|
}
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// getLocation returns the storage location for the given name and type.
|
||||||
|
func getLocation(name, storageType string) (string, error) {
|
||||||
|
location := databasesStructure.ChildDir(name, 0700).ChildDir(storageType, 0700)
|
||||||
|
// check location
|
||||||
|
err := location.Ensure()
|
||||||
|
if err != nil {
|
||||||
|
return "", fmt.Errorf(`failed to create/check database dir "%s": %s`, location.Path, err)
|
||||||
|
}
|
||||||
|
return location.Path, nil
|
||||||
|
}
|
||||||
|
|
|
@ -104,7 +104,7 @@ func loadRegistry() error {
|
||||||
defer registryLock.Unlock()
|
defer registryLock.Unlock()
|
||||||
|
|
||||||
// read file
|
// read file
|
||||||
filePath := path.Join(rootDir, registryFileName)
|
filePath := path.Join(rootStructure.Path, registryFileName)
|
||||||
data, err := ioutil.ReadFile(filePath)
|
data, err := ioutil.ReadFile(filePath)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
if os.IsNotExist(err) {
|
if os.IsNotExist(err) {
|
||||||
|
@ -139,7 +139,7 @@ func saveRegistry(lock bool) error {
|
||||||
}
|
}
|
||||||
|
|
||||||
// write file
|
// write file
|
||||||
filePath := path.Join(rootDir, registryFileName)
|
filePath := path.Join(rootStructure.Path, registryFileName)
|
||||||
return ioutil.WriteFile(filePath, data, 0600)
|
return ioutil.WriteFile(filePath, data, 0600)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Add table
Reference in a new issue