From 682264acc5bbc491f343bbad07ad53a78296936f Mon Sep 17 00:00:00 2001 From: Daniel Date: Fri, 26 Apr 2019 11:38:14 +0200 Subject: [PATCH] Fix database prepping for windows --- database/location.go | 20 +++++++++++++------- database/main.go | 6 +++--- database/storage/badger/badger.go | 15 +++++++++++---- database/storage/badger/badger_test.go | 2 +- 4 files changed, 28 insertions(+), 15 deletions(-) diff --git a/database/location.go b/database/location.go index 80250cc..5d1a1be 100644 --- a/database/location.go +++ b/database/location.go @@ -4,7 +4,8 @@ import ( "errors" "fmt" "os" - "path" + "path/filepath" + "runtime" ) const ( @@ -15,12 +16,12 @@ var ( rootDir string ) -func ensureDirectory(dirPath string) error { +func ensureDirectory(dirPath string, permissions os.FileMode) error { // open dir dir, err := os.Open(dirPath) if err != nil { if os.IsNotExist(err) { - return os.MkdirAll(dirPath, 0700) + return os.MkdirAll(dirPath, permissions) } return err } @@ -33,9 +34,14 @@ func ensureDirectory(dirPath string) error { if !fileInfo.IsDir() { 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 } @@ -46,10 +52,10 @@ func GetDatabaseRoot() string { // getLocation returns the storage location for the given name and type. func getLocation(name, storageType string) (string, error) { - location := path.Join(rootDir, databasesSubDir, name, storageType) + location := filepath.Join(rootDir, databasesSubDir, name, storageType) // check location - err := ensureDirectory(location) + err := ensureDirectory(location, 0700) if err != nil { return "", fmt.Errorf("location (%s) invalid: %s", location, err) } diff --git a/database/main.go b/database/main.go index 3c818ed..988b826 100644 --- a/database/main.go +++ b/database/main.go @@ -3,7 +3,7 @@ package database import ( "errors" "fmt" - "path" + "path/filepath" "github.com/tevino/abool" ) @@ -28,14 +28,14 @@ func SetLocation(location string) (ok bool) { func Initialize() error { if initialized.SetToIf(false, true) { - err := ensureDirectory(rootDir) + err := ensureDirectory(rootDir, 0755) if err != nil { return fmt.Errorf("could not create/open database directory (%s): %s", rootDir, err) } err = loadRegistry() 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 diff --git a/database/storage/badger/badger.go b/database/storage/badger/badger.go index add2867..5670124 100644 --- a/database/storage/badger/badger.go +++ b/database/storage/badger/badger.go @@ -11,6 +11,7 @@ import ( "github.com/Safing/portbase/database/query" "github.com/Safing/portbase/database/record" "github.com/Safing/portbase/database/storage" + "github.com/Safing/portbase/log" ) // Badger database made pluggable for portbase. @@ -30,6 +31,12 @@ func NewBadger(name, location string) (storage.Interface, error) { opts.ValueDir = location 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 { return nil, err } @@ -59,10 +66,10 @@ func (b *Badger) Get(key string) (record.Record, error) { return nil, err } - // DO NOT check for this, as we got our own machanism for that. - // if item.IsDeletedOrExpired() { - // return nil, storage.ErrNotFound - // } + // return err if deleted or expired + if item.IsDeletedOrExpired() { + return nil, storage.ErrNotFound + } data, err := item.ValueCopy(nil) if err != nil { diff --git a/database/storage/badger/badger_test.go b/database/storage/badger/badger_test.go index 9a66172..f8891d3 100644 --- a/database/storage/badger/badger_test.go +++ b/database/storage/badger/badger_test.go @@ -101,7 +101,7 @@ func TestBadger(t *testing.T) { for _ = range it.Next { cnt++ } - if it.Err != nil { + if it.Err() != nil { t.Fatal(err) } if cnt != 1 {