scanner: Use context only for cancellation

When the scanner is slower than the actual backup, the tomb cancels the
context passed to Scan(), which then returns ctx.Err(). In the end, the
main function prints an error message that is not helpful ("Context
cancelled") and exits with an error code although no error occurred.

The code now ignores the error in the context and just uses it for
cancellation. The scanner is not supposed to return an error anyway.

Closes #1978
This commit is contained in:
Alexander Neumann 2018-09-08 18:35:49 +02:00
parent 6d9c008900
commit 1140950d7b
2 changed files with 5 additions and 19 deletions

View file

@ -52,20 +52,17 @@ func (s *Scanner) Scan(ctx context.Context, targets []string) error {
}
if ctx.Err() != nil {
return ctx.Err()
return nil
}
}
if ctx.Err() != nil {
return ctx.Err()
}
s.Result("", stats)
return nil
}
func (s *Scanner) scan(ctx context.Context, stats ScanStats, target string) (ScanStats, error) {
if ctx.Err() != nil {
return stats, ctx.Err()
return stats, nil
}
// exclude files by path before running stat to reduce number of lstat calls
@ -89,10 +86,6 @@ func (s *Scanner) scan(ctx context.Context, stats ScanStats, target string) (Sca
stats.Files++
stats.Bytes += uint64(fi.Size())
case fi.Mode().IsDir():
if ctx.Err() != nil {
return stats, ctx.Err()
}
names, err := readdirnames(s.FS, target)
if err != nil {
return stats, s.Error(target, fi, err)
@ -109,9 +102,6 @@ func (s *Scanner) scan(ctx context.Context, stats ScanStats, target string) (Sca
stats.Others++
}
if ctx.Err() != nil {
return stats, ctx.Err()
}
s.Result(target, stats)
return stats, nil
}

View file

@ -289,7 +289,7 @@ func TestScannerCancel(t *testing.T) {
"other": TestFile{Content: "other"},
}
result := ScanStats{Files: 2, Bytes: 6}
result := ScanStats{Files: 2, Dirs: 1, Bytes: 6}
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
@ -319,12 +319,8 @@ func TestScannerCancel(t *testing.T) {
}
err = sc.Scan(ctx, []string{"."})
if err == nil {
t.Errorf("did not find expected error")
}
if err != context.Canceled {
t.Errorf("unexpected error found, want %v, got %v", context.Canceled, err)
if err != nil {
t.Errorf("unexpected error %v found", err)
}
if lastStats != result {