From 9abfd30ca9b86de13ae0c0459e363669a8541bf9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bal=C3=A1zs=20Grill?= Date: Sun, 4 Aug 2024 07:41:12 +0200 Subject: [PATCH] Ability to handle multiple projections in a process --- bindings/basepathfs.go | 3 +++ bindings/bindings.go | 31 ++++++++++++++++------ cmd/main/main.go | 57 ++++++++++++++++++++++++++++++++++++++++ cmd/s3/main.go | 7 ++--- filesystem/filesystem.go | 7 ++++- 5 files changed, 93 insertions(+), 12 deletions(-) create mode 100644 cmd/main/main.go diff --git a/bindings/basepathfs.go b/bindings/basepathfs.go index 3b45d8c..916d5dd 100644 --- a/bindings/basepathfs.go +++ b/bindings/basepathfs.go @@ -1,3 +1,6 @@ +/* +This file is a fork of https://github.com/spf13/afero/blob/master/basepath.go modified to always use "/" as separator regardless of the host system +*/ package bindings import ( diff --git a/bindings/bindings.go b/bindings/bindings.go index 0bbb318..34ba79e 100644 --- a/bindings/bindings.go +++ b/bindings/bindings.go @@ -2,6 +2,7 @@ package bindings import ( "flag" + "io" "log" "os" "os/signal" @@ -71,12 +72,27 @@ func ReadConfigFromRegistry(key registry.Key, config any) error { return nil } -func BindVirtualizationInstance(localpath string, remotefs afero.Fs) error { +func CloseOnSigTerm(closers ...io.Closer) { c := make(chan os.Signal, 1) signal.Notify(c, os.Interrupt, syscall.SIGTERM) + + <-c + for _, closer := range closers { + closer.Close() + } + os.Exit(1) +} + +type closerFunc func() error + +func (f closerFunc) Close() error { + return f() +} + +func BindVirtualizationInstance(localpath string, remotefs afero.Fs) (io.Closer, error) { closer, err := filesystem.StartProjecting(localpath, remotefs) if err != nil { - return err + return nil, err } t := time.NewTicker(30 * time.Second) @@ -84,14 +100,13 @@ func BindVirtualizationInstance(localpath string, remotefs afero.Fs) error { for range t.C { err = closer.PerformSynchronization() if err != nil { - log.Panic(err) + log.Println(err) } } }() - <-c - t.Stop() - closer.Close() - os.Exit(1) - return nil + return (closerFunc)(func() error { + t.Stop() + return closer.Close() + }), nil } diff --git a/cmd/main/main.go b/cmd/main/main.go new file mode 100644 index 0000000..21e9c1d --- /dev/null +++ b/cmd/main/main.go @@ -0,0 +1,57 @@ +package main + +import ( + "io" + "log" + + "github.com/balazsgrill/potatodrive/bindings" + cs3 "github.com/balazsgrill/potatodrive/bindings/s3" + "golang.org/x/sys/windows/registry" +) + +func main() { + parentkey, err := registry.OpenKey(registry.LOCAL_MACHINE, "SOFTWARE\\PotatoDrive", registry.QUERY_VALUE|registry.READ) + if err != nil { + panic(err) + } + + keys, err := parentkey.ReadSubKeyNames(0) + if err != nil { + panic(err) + } + + var instances []io.Closer + + for _, keyname := range keys { + config := &cs3.Config{} + key, err := registry.OpenKey(parentkey, keyname, registry.QUERY_VALUE) + if err != nil { + log.Printf("Open key: %v", err) + continue + } + bindings.ReadConfigFromRegistry(key, config) + err = config.Validate() + if err != nil { + log.Printf("Validate config: %v", err) + continue + } + fs, err := config.ToFileSystem() + if err != nil { + log.Printf("Create file system: %v", err) + continue + } + + log.Printf("Starting %s on %s", keyname, config.LocalPath) + c, err := bindings.BindVirtualizationInstance(config.LocalPath, fs) + if err != nil { + log.Println(err) + } + log.Printf("%s ended", keyname) + instances = append(instances, c) + + } + + bindings.CloseOnSigTerm(instances...) + + select {} +} diff --git a/cmd/s3/main.go b/cmd/s3/main.go index d95e54b..311694b 100644 --- a/cmd/s3/main.go +++ b/cmd/s3/main.go @@ -10,13 +10,13 @@ import ( ) func main() { - regkey := flag.String("regkey", "", "Registry key that holds configuration") + regkey := flag.String("regkey", "", "Registry key that holds configuration. If set, all other arguments are ignored") config := &cs3.Config{} bindings.ConfigToFlags(config) flag.Parse() if *regkey != "" { - key, err := registry.OpenKey(registry.CURRENT_USER, *regkey, registry.QUERY_VALUE) + key, err := registry.OpenKey(registry.LOCAL_MACHINE, *regkey, registry.QUERY_VALUE) if err != nil { log.Panic(err) } @@ -33,8 +33,9 @@ func main() { log.Panic(err) } - err = bindings.BindVirtualizationInstance(config.LocalPath, fs) + closer, err := bindings.BindVirtualizationInstance(config.LocalPath, fs) if err != nil { log.Panic(err) } + bindings.CloseOnSigTerm(closer) } diff --git a/filesystem/filesystem.go b/filesystem/filesystem.go index 52fef82..2bd261a 100644 --- a/filesystem/filesystem.go +++ b/filesystem/filesystem.go @@ -93,7 +93,12 @@ func (instance *VirtualizationInstance) start(rootPath string, filesystem afero. log.Printf("Error starting virtualization: %s", err) return err } - return instance.syncRemoteToLocal() + err = instance.syncRemoteToLocal() + if err != nil { + log.Printf("Initial sync failed: %s", err) + return nil + } + return nil } func (instance *VirtualizationInstance) path_localToRemote(path string) string {