fix: local count

This commit is contained in:
Urko. 2023-09-13 21:18:57 +02:00
parent bec9807a75
commit 083762e1c5
2 changed files with 17 additions and 7 deletions

View File

@ -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:

View File

@ -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())
}