From 083762e1c51d2a365800243716d072dd5d4f9daa Mon Sep 17 00:00:00 2001 From: "Urko." Date: Wed, 13 Sep 2023 21:18:57 +0200 Subject: [PATCH] fix: local count --- cmd/check.go | 12 +++++++----- internal/services/backblaze/check.go | 12 ++++++++++-- 2 files changed, 17 insertions(+), 7 deletions(-) diff --git a/cmd/check.go b/cmd/check.go index 165e586..550abcc 100644 --- a/cmd/check.go +++ b/cmd/check.go @@ -72,16 +72,17 @@ var Check = &cobra.Command{ logger.Info("start check") defer logger.Info("end check") + localChan := make(chan backblaze.B2Local) b2Chan := make(chan backblaze.B2Local) - doneChan := make(chan interface{}, 1) - go b.CompareConcurrent(ctx, backupDir, bucketName, localChan, b2Chan, doneChan) + countChan := make(chan int, 1) + go b.CompareConcurrent(ctx, backupDir, bucketName, localChan, b2Chan, countChan) reportBuilder := strings.Builder{} + reportBuilder.WriteString(fmt.Sprintf("Local files within `%s` path already in `%s` bucket:\n", backupDir, bucketName)) + countLocal := 0 countB2 := 0 - countOK := 0 - reportBuilder.WriteString(fmt.Sprintf("Local files within `%s` path already in `%s` bucket:\n", backupDir, bucketName)) cloudBuilder := strings.Builder{} cloudBuilder.WriteString(fmt.Sprintf("B2 files within `%s` bucket\n", bucketName)) @@ -95,6 +96,7 @@ var Check = &cobra.Command{ localBuilder.WriteString("|----------------------------------------------------------|------------------|--------------------------------|------------------------------------------\n") countNotInCloud := 0 + countOK := 0 loop: for { select { @@ -106,7 +108,7 @@ var Check = &cobra.Command{ logger.Error("Operation canceled") } break loop - case <-doneChan: + case countOK = <-countChan: logger.Debugln("done chan") break loop case localMsg, ok := <-localChan: diff --git a/internal/services/backblaze/check.go b/internal/services/backblaze/check.go index d0f866d..61ed3df 100644 --- a/internal/services/backblaze/check.go +++ b/internal/services/backblaze/check.go @@ -8,6 +8,7 @@ import ( "path" "path/filepath" "sync" + "sync/atomic" "time" "github.com/kurin/blazer/b2" @@ -93,7 +94,12 @@ type B2Local struct { // CompareConcurrent concurrently fetches the list of local files and cloud files, // then compares them to ensure all local files exist in the cloud. // Errors are sent to a provided error channel. The function will panic if an error occurs while listing files. -func (b *BackBlaze) CompareConcurrent(ctx context.Context, backupDir, bucketName string, localChan chan<- B2Local, b2Chan chan<- B2Local, doneChan chan<- interface{}) { +func (b *BackBlaze) CompareConcurrent( + ctx context.Context, + backupDir, bucketName string, + localChan, b2Chan chan<- B2Local, + doneChan chan<- int, +) { var wg sync.WaitGroup localFiles := make(map[string]File) cloudFiles := make(map[string]File) @@ -145,6 +151,7 @@ func (b *BackBlaze) CompareConcurrent(ctx context.Context, backupDir, bucketName wg.Wait() // Now check local files that are not present in cloud + var count atomic.Int64 wg.Add(2) go func() { defer wg.Done() @@ -155,6 +162,7 @@ func (b *BackBlaze) CompareConcurrent(ctx context.Context, backupDir, bucketName } b.logger.Debugf("localFile %+v\n", localFile) localChan <- B2Local{File: localFile, Err: nil} + count.Add(1) } }() @@ -172,5 +180,5 @@ func (b *BackBlaze) CompareConcurrent(ctx context.Context, backupDir, bucketName wg.Wait() close(localChan) close(b2Chan) - doneChan <- nil + doneChan <- int(count.Load()) }