From 86a99224ac23156ed6c2f8bb0eadb27e6d94c143 Mon Sep 17 00:00:00 2001 From: "Marcel M. Otte" Date: Sun, 14 Nov 2021 15:07:20 +0100 Subject: [PATCH] Another deve status committed... --- cmd/backive_udev/main.go | 1 + core/backup.go | 40 ++++++++++++++++++++++++++++++++++++ core/device.go | 9 ++++++++ events/events.go | 44 +++++++++++++++++++++++++++++++--------- 4 files changed, 84 insertions(+), 10 deletions(-) diff --git a/cmd/backive_udev/main.go b/cmd/backive_udev/main.go index 09823c5..dc2c453 100644 --- a/cmd/backive_udev/main.go +++ b/cmd/backive_udev/main.go @@ -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 { diff --git a/core/backup.go b/core/backup.go index 52d0478..cd7ed11 100644 --- a/core/backup.go +++ b/core/backup.go @@ -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") +} diff --git a/core/device.go b/core/device.go index d21ce91..79b06f9 100644 --- a/core/device.go +++ b/core/device.go @@ -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 } diff --git a/events/events.go b/events/events.go index 3f1b829..a432ca9 100644 --- a/events/events.go +++ b/events/events.go @@ -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] }