mirror of
https://github.com/safing/portbase
synced 2025-09-04 11:40:23 +00:00
Add util function for creating/checking dirs
This commit is contained in:
parent
13e64209a6
commit
0de310503a
4 changed files with 51 additions and 35 deletions
|
@ -1,11 +1,10 @@
|
||||||
package database
|
package database
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"errors"
|
|
||||||
"fmt"
|
"fmt"
|
||||||
"os"
|
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
"runtime"
|
|
||||||
|
"github.com/safing/portbase/utils"
|
||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
|
@ -16,35 +15,6 @@ var (
|
||||||
rootDir string
|
rootDir string
|
||||||
)
|
)
|
||||||
|
|
||||||
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, permissions)
|
|
||||||
}
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
defer dir.Close()
|
|
||||||
|
|
||||||
fileInfo, err := dir.Stat()
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
if !fileInfo.IsDir() {
|
|
||||||
return errors.New("path exists and is not a directory")
|
|
||||||
}
|
|
||||||
|
|
||||||
if runtime.GOOS == "windows" {
|
|
||||||
// TODO
|
|
||||||
// acl.Chmod(dirPath, permissions)
|
|
||||||
} else if fileInfo.Mode().Perm() != permissions {
|
|
||||||
return dir.Chmod(permissions)
|
|
||||||
}
|
|
||||||
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// GetDatabaseRoot returns the root directory of the database.
|
// GetDatabaseRoot returns the root directory of the database.
|
||||||
func GetDatabaseRoot() string {
|
func GetDatabaseRoot() string {
|
||||||
return rootDir
|
return rootDir
|
||||||
|
@ -55,7 +25,7 @@ func getLocation(name, storageType string) (string, error) {
|
||||||
location := filepath.Join(rootDir, databasesSubDir, name, storageType)
|
location := filepath.Join(rootDir, databasesSubDir, name, storageType)
|
||||||
|
|
||||||
// check location
|
// check location
|
||||||
err := ensureDirectory(location, 0700)
|
err := utils.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)
|
||||||
}
|
}
|
||||||
|
|
|
@ -5,6 +5,7 @@ import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
|
|
||||||
|
"github.com/safing/portbase/utils"
|
||||||
"github.com/tevino/abool"
|
"github.com/tevino/abool"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -28,7 +29,7 @@ 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, 0755)
|
err := utils.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)
|
||||||
}
|
}
|
||||||
|
|
|
@ -56,7 +56,10 @@ func log(level severity, msg string, trace *ContextTracer) {
|
||||||
// check if level is enabled for file or generally
|
// check if level is enabled for file or generally
|
||||||
if fileLevelsActive.IsSet() {
|
if fileLevelsActive.IsSet() {
|
||||||
fileOnly := strings.Split(file, "/")
|
fileOnly := strings.Split(file, "/")
|
||||||
sev, ok := fileLevels[fileOnly[len(fileOnly)-1]]
|
if len(fileOnly) < 2 {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
sev, ok := fileLevels[fileOnly[len(fileOnly)-2]]
|
||||||
if ok {
|
if ok {
|
||||||
if level < sev {
|
if level < sev {
|
||||||
return
|
return
|
||||||
|
|
42
utils/fs.go
Normal file
42
utils/fs.go
Normal file
|
@ -0,0 +1,42 @@
|
||||||
|
package utils
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"os"
|
||||||
|
"runtime"
|
||||||
|
)
|
||||||
|
|
||||||
|
// EnsureDirectory ensures that the given directoy exists and that is has the given permissions set.
|
||||||
|
// If path is a file, it is deleted and a directory created.
|
||||||
|
// If a directory is created, also all missing directories up to the required one are created with the given permissions.
|
||||||
|
func EnsureDirectory(path string, perm os.FileMode) error {
|
||||||
|
// open path
|
||||||
|
f, err := os.Stat(path)
|
||||||
|
if err == nil {
|
||||||
|
// file exists
|
||||||
|
if f.IsDir() {
|
||||||
|
// directory exists, check permissions
|
||||||
|
if runtime.GOOS == "windows" {
|
||||||
|
// TODO: set correct permission on windows
|
||||||
|
// acl.Chmod(path, perm)
|
||||||
|
} else if f.Mode().Perm() != perm {
|
||||||
|
return os.Chmod(path, perm)
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
err = os.Remove(path)
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("could not remove file %s to place dir: %s", path, err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// file does not exist (or has been deleted)
|
||||||
|
if err == nil || os.IsNotExist(err) {
|
||||||
|
err = os.MkdirAll(path, perm)
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("could not create dir %s: %s", path, err)
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
// other error opening path
|
||||||
|
return fmt.Errorf("failed to access %s: %s", path, err)
|
||||||
|
}
|
Loading…
Add table
Reference in a new issue