add data folder permission validation

agent will not boot up if not having any permissions
This commit is contained in:
Cedric Verstraeten
2022-09-20 21:37:35 +02:00
parent 784fe73a55
commit 16e4c68fb3
2 changed files with 43 additions and 0 deletions

View File

@@ -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.

View File

@@ -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 {