mirror of
https://github.com/safing/portbase
synced 2025-09-01 18:19:57 +00:00
Fix database prepping for windows
This commit is contained in:
parent
5908ff3d0b
commit
682264acc5
4 changed files with 28 additions and 15 deletions
|
@ -4,7 +4,8 @@ import (
|
||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"os"
|
"os"
|
||||||
"path"
|
"path/filepath"
|
||||||
|
"runtime"
|
||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
|
@ -15,12 +16,12 @@ var (
|
||||||
rootDir string
|
rootDir string
|
||||||
)
|
)
|
||||||
|
|
||||||
func ensureDirectory(dirPath string) error {
|
func ensureDirectory(dirPath string, permissions os.FileMode) error {
|
||||||
// open dir
|
// open dir
|
||||||
dir, err := os.Open(dirPath)
|
dir, err := os.Open(dirPath)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
if os.IsNotExist(err) {
|
if os.IsNotExist(err) {
|
||||||
return os.MkdirAll(dirPath, 0700)
|
return os.MkdirAll(dirPath, permissions)
|
||||||
}
|
}
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
@ -33,9 +34,14 @@ func ensureDirectory(dirPath string) error {
|
||||||
if !fileInfo.IsDir() {
|
if !fileInfo.IsDir() {
|
||||||
return errors.New("path exists and is not a directory")
|
return errors.New("path exists and is not a directory")
|
||||||
}
|
}
|
||||||
if fileInfo.Mode().Perm() != 0700 {
|
|
||||||
return dir.Chmod(0700)
|
if runtime.GOOS == "windows" {
|
||||||
|
// TODO
|
||||||
|
// acl.Chmod(dirPath, permissions)
|
||||||
|
} else if fileInfo.Mode().Perm() != permissions {
|
||||||
|
return dir.Chmod(permissions)
|
||||||
}
|
}
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -46,10 +52,10 @@ func GetDatabaseRoot() string {
|
||||||
|
|
||||||
// getLocation returns the storage location for the given name and type.
|
// getLocation returns the storage location for the given name and type.
|
||||||
func getLocation(name, storageType string) (string, error) {
|
func getLocation(name, storageType string) (string, error) {
|
||||||
location := path.Join(rootDir, databasesSubDir, name, storageType)
|
location := filepath.Join(rootDir, databasesSubDir, name, storageType)
|
||||||
|
|
||||||
// check location
|
// check location
|
||||||
err := ensureDirectory(location)
|
err := ensureDirectory(location, 0700)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return "", fmt.Errorf("location (%s) invalid: %s", location, err)
|
return "", fmt.Errorf("location (%s) invalid: %s", location, err)
|
||||||
}
|
}
|
||||||
|
|
|
@ -3,7 +3,7 @@ package database
|
||||||
import (
|
import (
|
||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"path"
|
"path/filepath"
|
||||||
|
|
||||||
"github.com/tevino/abool"
|
"github.com/tevino/abool"
|
||||||
)
|
)
|
||||||
|
@ -28,14 +28,14 @@ func SetLocation(location string) (ok bool) {
|
||||||
func Initialize() error {
|
func Initialize() error {
|
||||||
if initialized.SetToIf(false, true) {
|
if initialized.SetToIf(false, true) {
|
||||||
|
|
||||||
err := ensureDirectory(rootDir)
|
err := ensureDirectory(rootDir, 0755)
|
||||||
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", rootDir, err)
|
||||||
}
|
}
|
||||||
|
|
||||||
err = loadRegistry()
|
err = loadRegistry()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("could not load database registry (%s): %s", path.Join(rootDir, registryFileName), err)
|
return fmt.Errorf("could not load database registry (%s): %s", filepath.Join(rootDir, registryFileName), err)
|
||||||
}
|
}
|
||||||
|
|
||||||
// start registry writer
|
// start registry writer
|
||||||
|
|
|
@ -11,6 +11,7 @@ import (
|
||||||
"github.com/Safing/portbase/database/query"
|
"github.com/Safing/portbase/database/query"
|
||||||
"github.com/Safing/portbase/database/record"
|
"github.com/Safing/portbase/database/record"
|
||||||
"github.com/Safing/portbase/database/storage"
|
"github.com/Safing/portbase/database/storage"
|
||||||
|
"github.com/Safing/portbase/log"
|
||||||
)
|
)
|
||||||
|
|
||||||
// Badger database made pluggable for portbase.
|
// Badger database made pluggable for portbase.
|
||||||
|
@ -30,6 +31,12 @@ func NewBadger(name, location string) (storage.Interface, error) {
|
||||||
opts.ValueDir = location
|
opts.ValueDir = location
|
||||||
|
|
||||||
db, err := badger.Open(opts)
|
db, err := badger.Open(opts)
|
||||||
|
if err == badger.ErrTruncateNeeded {
|
||||||
|
// clean up after crash
|
||||||
|
log.Warningf("database/storage: truncating corrupted value log of badger database %s: this may cause data loss", name)
|
||||||
|
opts.Truncate = true
|
||||||
|
db, err = badger.Open(opts)
|
||||||
|
}
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
@ -59,10 +66,10 @@ func (b *Badger) Get(key string) (record.Record, error) {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
// DO NOT check for this, as we got our own machanism for that.
|
// return err if deleted or expired
|
||||||
// if item.IsDeletedOrExpired() {
|
if item.IsDeletedOrExpired() {
|
||||||
// return nil, storage.ErrNotFound
|
return nil, storage.ErrNotFound
|
||||||
// }
|
}
|
||||||
|
|
||||||
data, err := item.ValueCopy(nil)
|
data, err := item.ValueCopy(nil)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|
|
@ -101,7 +101,7 @@ func TestBadger(t *testing.T) {
|
||||||
for _ = range it.Next {
|
for _ = range it.Next {
|
||||||
cnt++
|
cnt++
|
||||||
}
|
}
|
||||||
if it.Err != nil {
|
if it.Err() != nil {
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
}
|
}
|
||||||
if cnt != 1 {
|
if cnt != 1 {
|
||||||
|
|
Loading…
Add table
Reference in a new issue