Another deve status committed...

This commit is contained in:
Marcel M. Otte 2021-11-14 15:07:20 +01:00
parent 6479744b4e
commit 86a99224ac
4 changed files with 84 additions and 10 deletions

View File

@ -9,6 +9,7 @@ import (
"strings"
)
// main Simple main function for the udev callback executable, registered with the udev service.
func main() {
f, err := os.OpenFile("/tmp/backive/udev.log", os.O_APPEND|os.O_CREATE|os.O_RDWR, 0666)
if err != nil {

View File

@ -1,5 +1,12 @@
package core
import (
"fmt"
"os"
"github.com/qwc/backive/config"
)
// Backup contains all necessary information for executing a configured backup.
type Backup struct {
Name string `mapstructure:",omitempty"`
@ -10,3 +17,36 @@ type Backup struct {
Frequency int `mapstructure:"frequency"`
ExeUser string `mapstructure:"user,omitempty"`
}
// Run runs the backup script with appropriate rights.
func (b Backup) Run() error {
cfg := config.Get()
if cfg.Devices[b.Name].isMounted() {
checkExistence := func(path string, name string) error {
if _, err := os.Stat(path); err != nil {
if os.IsNotExist(err) {
return fmt.Errorf("%s does not exist", name)
} else {
return fmt.Errorf("Error when checking %s: %w", name, err)
}
}
return nil
}
// Check for existence of target dir
if err := checkExistence(b.TargetDir, "target directory"); err != nil {
return err
}
// check for existence of source dir
if err := checkExistence(b.SourceDir, "source directory"); err != nil {
return err
}
// check for existence of script path
if err := checkExistence(b.ScriptPath, "script path"); err != nil {
return err
}
// setup script environment including user to use
// run script
}
// quit with error that the device is not available.
return fmt.Errorf("The device is not mounted")
}

View File

@ -5,12 +5,21 @@ type Device struct {
Name string `mapstructure:",omitempty"`
UUID string `mapstructure:"uuid"`
OwnerUser string `mapstructure:"owner,omitempty"`
isMounted bool
}
// Mount will mount a device
func (d Device) Mount() {
d.isMounted = true
}
// Unmount will unmount a device
func (d Device) Unmount() {
d.isMounted = false
}
func (d Device) IsMounted() bool {
return d.isMounted
}

View File

@ -1,10 +1,16 @@
package events
import "net"
import (
"encoding/json"
"io"
"net"
)
var ls net.Listener
var done <-chan struct{}
var callbacks := make([]func(map[string]string), 3)
// Init initializes the unix socket.
func Init(socketPath string) {
ls, err = net.Listen(socketPath)
if err != nil {
@ -12,26 +18,44 @@ func Init(socketPath string) {
}
}
func RunLoop() {
// Listen starts the event loop.
func Listen() {
for {
go func() {
process()
}()
}
}
// RegisterCallback adds a function to the list of callback functions for processing of events.
func RegisterCallback(cb func(map[string]string)){
append(callbacks, cb)
}
// process processes each and every unix socket event, Unmarshals the json data and calls the list of callbacks.
func process() {
client, err = ls.Accept()
if err != nil {
panic(err)
}
//TODO: rewrite to be safe regarding buffer length
buf := make([]byte, 2048)
nr, err := client.Read(buf)
if err != nil {
return
data := make([]byte, 2048)
for {
buf := make([]byte, 512)
nr, err := client.Read(buf)
if err != nil && err != io.EOF {
panic(err)
}
append(data, buf[0:nr])
if err == io.EOF {
break
}
}
env := map[string]string{}
err := json.Unmarshal(data, &env)
if err != nil {
panic(err)
}
for _, v = range(callbacks) {
v(env)
}
data := buf[0:nr]
}