Use atomic operations for failure tracking in ProcessCheck

Replace the non-atomic 'bad' bool with atomic comparison of FilesFailed
count before and after the walk. This ensures consistent use of atomic
operations for all shared state and eliminates a potential race if
parallelism is added in the future.
This commit is contained in:
Jeffrey Paul 2026-02-02 13:49:12 -08:00
parent 9f86bf1dc1
commit 5c2338d590

View File

@ -407,13 +407,13 @@ func newCheckCmd() *cobra.Command {
func ProcessCheck(dir string, cont bool, stats *Stats, bar *progressbar.ProgressBar) error { func ProcessCheck(dir string, cont bool, stats *Stats, bar *progressbar.ProgressBar) error {
fail := errors.New("verification failed") fail := errors.New("verification failed")
bad := false // Track initial failed count to detect failures during this walk
initialFailed := atomic.LoadInt64(&stats.FilesFailed)
err := walkAndProcess(dir, stats, bar, func(p string, info os.FileInfo, s *Stats) error { err := walkAndProcess(dir, stats, bar, func(p string, info os.FileInfo, s *Stats) error {
exp, err := xattr.Get(p, checksumKey) exp, err := xattr.Get(p, checksumKey)
if err != nil { if err != nil {
if errors.Is(err, xattr.ENOATTR) { if errors.Is(err, xattr.ENOATTR) {
bad = true
atomic.AddInt64(&s.FilesFailed, 1) atomic.AddInt64(&s.FilesFailed, 1)
if verbose && !quiet { if verbose && !quiet {
fmt.Printf("%s <none> ERROR\n", p) fmt.Printf("%s <none> ERROR\n", p)
@ -433,7 +433,6 @@ func ProcessCheck(dir string, cont bool, stats *Stats, bar *progressbar.Progress
} }
ok := bytes.Equal(exp, act) ok := bytes.Equal(exp, act)
if !ok { if !ok {
bad = true
atomic.AddInt64(&s.FilesFailed, 1) atomic.AddInt64(&s.FilesFailed, 1)
} else { } else {
atomic.AddInt64(&s.FilesProcessed, 1) atomic.AddInt64(&s.FilesProcessed, 1)
@ -458,7 +457,8 @@ func ProcessCheck(dir string, cont bool, stats *Stats, bar *progressbar.Progress
} }
return err return err
} }
if bad { // Check if any failures occurred during this walk
if atomic.LoadInt64(&stats.FilesFailed) > initialFailed {
return fail return fail
} }
return nil return nil