From 280d580ae2dce06a00c71cf579995a5551b0195f Mon Sep 17 00:00:00 2001 From: Alexander Neumann Date: Sun, 24 Jan 2016 00:14:15 +0100 Subject: [PATCH] checker: Use Load() instead of GetReader() --- checker/checker.go | 14 ++------------ checker/checker_test.go | 38 ++++++++++++-------------------------- 2 files changed, 14 insertions(+), 38 deletions(-) diff --git a/checker/checker.go b/checker/checker.go index 97577285d..97fe8df58 100644 --- a/checker/checker.go +++ b/checker/checker.go @@ -4,7 +4,6 @@ import ( "bytes" "errors" "fmt" - "io/ioutil" "sync" "github.com/restic/restic" @@ -647,17 +646,8 @@ func (c *Checker) CountPacks() uint64 { // checkPack reads a pack and checks the integrity of all blobs. func checkPack(r *repository.Repository, id backend.ID) error { debug.Log("Checker.checkPack", "checking pack %v", id.Str()) - rd, err := r.Backend().GetReader(backend.Data, id.String(), 0, 0) - if err != nil { - return err - } - - buf, err := ioutil.ReadAll(rd) - if err != nil { - return err - } - - err = rd.Close() + h := backend.Handle{Type: backend.Data, Name: id.String()} + buf, err := backend.LoadAll(r.Backend(), h, nil) if err != nil { return err } diff --git a/checker/checker_test.go b/checker/checker_test.go index 6118b987d..10b37e219 100644 --- a/checker/checker_test.go +++ b/checker/checker_test.go @@ -1,7 +1,7 @@ package checker_test import ( - "io" + "fmt" "math/rand" "path/filepath" "sort" @@ -213,24 +213,22 @@ func TestDuplicatePacksInIndex(t *testing.T) { // errorBackend randomly modifies data after reading. type errorBackend struct { backend.Backend + ProduceErrors bool } -func (b errorBackend) GetReader(t backend.Type, name string, offset, length uint) (io.ReadCloser, error) { - rd, err := b.Backend.GetReader(t, name, offset, length) - if err != nil { - return rd, err - } +func (b errorBackend) Load(h backend.Handle, p []byte, off int64) (int, error) { + fmt.Printf("load %v\n", h) + n, err := b.Backend.Load(h, p, off) - if t != backend.Data { - return rd, err + if b.ProduceErrors { + induceError(p) } - - return backend.ReadCloser(faultReader{rd}), nil + return n, err } // induceError flips a bit in the slice. func induceError(data []byte) { - if rand.Float32() < 0.8 { + if rand.Float32() < 0.2 { return } @@ -238,20 +236,6 @@ func induceError(data []byte) { data[pos] ^= 1 } -// faultReader wraps a reader and randomly modifies data on read. -type faultReader struct { - rd io.Reader -} - -func (f faultReader) Read(p []byte) (int, error) { - n, err := f.rd.Read(p) - if n > 0 { - induceError(p) - } - - return n, err -} - func TestCheckerModifiedData(t *testing.T) { be := mem.New() @@ -263,7 +247,8 @@ func TestCheckerModifiedData(t *testing.T) { OK(t, err) t.Logf("archived as %v", id.Str()) - checkRepo := repository.New(errorBackend{be}) + beError := &errorBackend{Backend: be} + checkRepo := repository.New(beError) OK(t, checkRepo.SearchKey(TestPassword)) chkr := checker.New(checkRepo) @@ -277,6 +262,7 @@ func TestCheckerModifiedData(t *testing.T) { t.Errorf("expected no hints, got %v: %v", len(hints), hints) } + beError.ProduceErrors = true errFound := false for _, err := range checkPacks(chkr) { t.Logf("pack error: %v", err)