From a47c4e5bacfcf82c956b0d0b00142306b141d819 Mon Sep 17 00:00:00 2001 From: aleskandro Date: Sat, 31 Aug 2024 11:49:15 -0400 Subject: [PATCH] Fixes --- main.go | 2 +- pkg/contoller.go | 17 +++++++++-------- pkg/syncer.go | 9 ++++----- pkg/utils.go | 14 ++------------ 4 files changed, 16 insertions(+), 26 deletions(-) diff --git a/main.go b/main.go index e71bb02..d5c18f0 100644 --- a/main.go +++ b/main.go @@ -22,8 +22,8 @@ func main() { log.Println(err) return } - controller := pkg.NewNetworkConnectionReconciler(config) ctx := SetupSignalHandler() + controller := pkg.NewNetworkConnectionReconciler(config, ctx) if *sync { controller.HandleWmNetworkConnected(ctx) } diff --git a/pkg/contoller.go b/pkg/contoller.go index 8bb5bce..9416bea 100644 --- a/pkg/contoller.go +++ b/pkg/contoller.go @@ -22,7 +22,7 @@ type NetworkConnectionReconciler struct { var networkConnectionFailedErr = fmt.Errorf("network connection failed") -func NewNetworkConnectionReconciler(config *Config) *NetworkConnectionReconciler { +func NewNetworkConnectionReconciler(config *Config, ctx context.Context) *NetworkConnectionReconciler { conn, err := dbus.SystemBus() if err != nil { log.Fatalf("Failed to connect to system bus: %v", err) @@ -34,12 +34,15 @@ func NewNetworkConnectionReconciler(config *Config) *NetworkConnectionReconciler log.Fatalf("Failed to add D-Bus match: %v", call.Err) } conn.Signal(ch) - return &NetworkConnectionReconciler{ + n := &NetworkConnectionReconciler{ conn: conn, dbusChan: ch, config: config, toastsChan: make(chan string, 16), + wg: &sync.WaitGroup{}, } + go n.dispatchMessages(ctx) + return n } func (n *NetworkConnectionReconciler) Run(ctx context.Context) { @@ -82,15 +85,11 @@ func (n *NetworkConnectionReconciler) HandleWmNetworkConnected(ctx context.Conte } n.wg.Wait() n.syncCtx, n.syncCtxCancel = context.WithCancel(ctx) - n.wg.Add(3) + n.wg.Add(2) go func() { defer n.wg.Done() n.keepNetworkAlive(n.syncCtx) }() - go func() { - defer n.wg.Done() - n.dispatchMessages(n.syncCtx) - }() go func() { defer n.wg.Done() defer func() { @@ -99,7 +98,7 @@ func (n *NetworkConnectionReconciler) HandleWmNetworkConnected(ctx context.Conte n.syncCtxCancel = nil } }() - n.sync(ctx) + n.sync(n.syncCtx) if n.config.AutoUpdate { n.updateNow() } @@ -112,6 +111,7 @@ func (n *NetworkConnectionReconciler) keepNetworkAlive(ctx context.Context) { for { select { case <-ctx.Done(): + log.Println("[keepNetworkAlive] context closed") return case <-ticker.C: obj := n.conn.Object("com.github.shermp.nickeldbus", "/nickeldbus") @@ -127,6 +127,7 @@ func (n *NetworkConnectionReconciler) dispatchMessages(ctx context.Context) { for { select { case <-ctx.Done(): + log.Println("[dispatchMessages] context closed") return case message := <-n.toastsChan: n.notifyNickel(message) diff --git a/pkg/syncer.go b/pkg/syncer.go index defe899..bf0d721 100644 --- a/pkg/syncer.go +++ b/pkg/syncer.go @@ -98,7 +98,8 @@ func (n *NetworkConnectionReconciler) syncFolder(client *gowebdav.Client, ctx co return } var updatedFilesRec []string - updatedFilesRec, err = n.syncFolder(client, ctx, remoteFilePath+"/", localFilePath) + localFileMap[localFilePath] = localFilePath + updatedFilesRec, err = n.syncFolder(client, ctx, remoteFilePath, localFilePath) updatedFiles = append(updatedFiles, updatedFilesRec...) if err != nil { return @@ -106,12 +107,10 @@ func (n *NetworkConnectionReconciler) syncFolder(client *gowebdav.Client, ctx co } else { localFileMap[localFilePath] = localFilePath if shouldDownloadFile(localFilePath, file.ModTime(), file.Size()) { - log.Printf("Downloading file %s to %s\n", remoteFilePath, localFilePath) if err = downloadFile(client, remoteFilePath, localFilePath); err != nil { return } updatedFiles = append(updatedFiles, localFilePath) - log.Println("Downloaded file", localFilePath) n.toastsChan <- fmt.Sprintf("Downloaded %s", remoteFilePath) } else { log.Println("Skipping file", remoteFilePath) @@ -238,9 +237,9 @@ func downloadFile(client *gowebdav.Client, remoteFilePath, localFilePath string) //nolint:errcheck defer localFileWriter.Close() - if _, err := io.Copy(localFileWriter, remoteFileReader); err != nil { + if _, err = io.Copy(localFileWriter, remoteFileReader); err != nil { return fmt.Errorf("error writing to local file %s: %w", localFilePath, err) } - + log.Println("Downloaded file", localFilePath) return nil } diff --git a/pkg/utils.go b/pkg/utils.go index 1ea96b0..2c83261 100644 --- a/pkg/utils.go +++ b/pkg/utils.go @@ -1,7 +1,6 @@ package pkg import ( - "fmt" "log" "os" "path" @@ -10,17 +9,8 @@ import ( func shouldDownloadFile(localFilePath string, remoteModTime time.Time, size int64) bool { info, err := os.Stat(localFilePath) - if os.IsNotExist(err) { - return true - } - if err != nil { - fmt.Println("Error getting local file info:", err) - return true - } - if info.Size() != size { - return true - } - return remoteModTime.After(info.ModTime()) + log.Println("Checking existence of ", localFilePath) + return err != nil || info.Size() != size || remoteModTime.After(info.ModTime()) } func removeRemotelyDeletedFiles(localFileMap map[string]string, localPath string) (err error) {