Skip to content

Commit

Permalink
Fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
aleskandro committed Aug 31, 2024
1 parent a8e7927 commit a47c4e5
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 26 deletions.
2 changes: 1 addition & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down
17 changes: 9 additions & 8 deletions pkg/contoller.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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) {
Expand Down Expand Up @@ -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() {
Expand All @@ -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()
}
Expand All @@ -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")
Expand All @@ -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)
Expand Down
9 changes: 4 additions & 5 deletions pkg/syncer.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,20 +98,19 @@ 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
}
} 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)
Expand Down Expand Up @@ -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
}
14 changes: 2 additions & 12 deletions pkg/utils.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package pkg

import (
"fmt"
"log"
"os"
"path"
Expand All @@ -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) {
Expand Down

0 comments on commit a47c4e5

Please sign in to comment.