mirror of
https://github.com/kerberos-io/agent.git
synced 2026-08-23 15:08:32 +00:00
add data folder permission validation
agent will not boot up if not having any permissions
This commit is contained in:
@@ -44,6 +44,13 @@ func main() {
|
||||
name := os.Args[2]
|
||||
port := os.Args[3]
|
||||
|
||||
// Check the folder permissions, it might be that we do not have permissions to write
|
||||
// recordings, update the configuration or save snapshots.
|
||||
err := utils.CheckDataDirectoryPermissions()
|
||||
if err != nil {
|
||||
log.Log.Fatal(err.Error())
|
||||
}
|
||||
|
||||
// Read the config on start, and pass it to the other
|
||||
// function and features. Please note that this might be changed
|
||||
// when saving or updating the configuration through the REST api or MQTT handler.
|
||||
|
||||
@@ -2,6 +2,7 @@ package utils
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"errors"
|
||||
"fmt"
|
||||
"io/ioutil"
|
||||
"math/rand"
|
||||
@@ -85,6 +86,41 @@ func CountDigits(i int64) (count int) {
|
||||
return count
|
||||
}
|
||||
|
||||
func CheckDataDirectoryPermissions() error {
|
||||
recordingsDirectory := "./data/recordings"
|
||||
configDirectory := "./data/config"
|
||||
snapshotsDirectory := "./data/snapshots"
|
||||
|
||||
err := CheckDirectoryPermissions(recordingsDirectory)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
err = CheckDirectoryPermissions(configDirectory)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
err = CheckDirectoryPermissions(snapshotsDirectory)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func CheckDirectoryPermissions(directory string) error {
|
||||
file := directory + "/.test"
|
||||
f, err := os.Create(file)
|
||||
defer f.Close()
|
||||
if err == nil {
|
||||
err := os.Remove(file)
|
||||
if err == nil {
|
||||
return nil
|
||||
} else {
|
||||
return errors.New("Problem deleting a file: " + err.Error())
|
||||
}
|
||||
}
|
||||
return errors.New("Problem creating a file: " + err.Error())
|
||||
}
|
||||
|
||||
func ReadDirectory(directory string) ([]os.FileInfo, error) {
|
||||
ff, err := ioutil.ReadDir(directory)
|
||||
if err != nil {
|
||||
|
||||
Reference in New Issue
Block a user