From b094d98c63cbfceb1d46aed13c606c19b13f213a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=B4=94=E7=AB=9E=E5=AE=81?= Date: Mon, 18 Sep 2023 12:30:59 +0800 Subject: [PATCH] fix: disable hash when all target is bad --- copy.go | 41 ++++++++++++++++++++--------------------- 1 file changed, 20 insertions(+), 21 deletions(-) diff --git a/copy.go b/copy.go index 3cf7b75..612e0b3 100644 --- a/copy.go +++ b/copy.go @@ -97,26 +97,6 @@ func (c *Copyer) write(ctx context.Context, job *writeJob, ch chan<- *baseJob, c } }() - if c.withHash { - sha := sha256Pool.Get().(hash.Hash) - sha.Reset() - - ch := make(chan []byte, 4) - chans = append(chans, ch) - - wg.Add(1) - go wrap(ctx, func() { - defer wg.Done() - defer sha256Pool.Put(sha) - - for buf := range ch { - sha.Write(buf) - } - - job.setHash(sha.Sum(nil)) - }) - } - var readErr error for _, target := range job.targets { dev := c.getDevice(target) @@ -195,10 +175,29 @@ func (c *Copyer) write(ctx context.Context, job *writeJob, ch chan<- *baseJob, c } }) } - if len(chans) == 0 { return } + + if c.withHash { + sha := sha256Pool.Get().(hash.Hash) + sha.Reset() + + ch := make(chan []byte, 4) + chans = append(chans, ch) + + wg.Add(1) + go wrap(ctx, func() { + defer wg.Done() + defer sha256Pool.Put(sha) + + for buf := range ch { + sha.Write(buf) + } + + job.setHash(sha.Sum(nil)) + }) + } readErr = c.streamCopy(ctx, chans, job.src, &cntr.bytes) }