checker: Use Load() instead of GetReader()

This commit is contained in:
Alexander Neumann 2016-01-24 00:14:15 +01:00
parent 782a1bf7b0
commit 280d580ae2
2 changed files with 14 additions and 38 deletions

View File

@ -4,7 +4,6 @@ import (
"bytes" "bytes"
"errors" "errors"
"fmt" "fmt"
"io/ioutil"
"sync" "sync"
"github.com/restic/restic" "github.com/restic/restic"
@ -647,17 +646,8 @@ func (c *Checker) CountPacks() uint64 {
// checkPack reads a pack and checks the integrity of all blobs. // checkPack reads a pack and checks the integrity of all blobs.
func checkPack(r *repository.Repository, id backend.ID) error { func checkPack(r *repository.Repository, id backend.ID) error {
debug.Log("Checker.checkPack", "checking pack %v", id.Str()) debug.Log("Checker.checkPack", "checking pack %v", id.Str())
rd, err := r.Backend().GetReader(backend.Data, id.String(), 0, 0) h := backend.Handle{Type: backend.Data, Name: id.String()}
if err != nil { buf, err := backend.LoadAll(r.Backend(), h, nil)
return err
}
buf, err := ioutil.ReadAll(rd)
if err != nil {
return err
}
err = rd.Close()
if err != nil { if err != nil {
return err return err
} }

View File

@ -1,7 +1,7 @@
package checker_test package checker_test
import ( import (
"io" "fmt"
"math/rand" "math/rand"
"path/filepath" "path/filepath"
"sort" "sort"
@ -213,24 +213,22 @@ func TestDuplicatePacksInIndex(t *testing.T) {
// errorBackend randomly modifies data after reading. // errorBackend randomly modifies data after reading.
type errorBackend struct { type errorBackend struct {
backend.Backend backend.Backend
ProduceErrors bool
} }
func (b errorBackend) GetReader(t backend.Type, name string, offset, length uint) (io.ReadCloser, error) { func (b errorBackend) Load(h backend.Handle, p []byte, off int64) (int, error) {
rd, err := b.Backend.GetReader(t, name, offset, length) fmt.Printf("load %v\n", h)
if err != nil { n, err := b.Backend.Load(h, p, off)
return rd, err
}
if t != backend.Data { if b.ProduceErrors {
return rd, err induceError(p)
} }
return n, err
return backend.ReadCloser(faultReader{rd}), nil
} }
// induceError flips a bit in the slice. // induceError flips a bit in the slice.
func induceError(data []byte) { func induceError(data []byte) {
if rand.Float32() < 0.8 { if rand.Float32() < 0.2 {
return return
} }
@ -238,20 +236,6 @@ func induceError(data []byte) {
data[pos] ^= 1 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) { func TestCheckerModifiedData(t *testing.T) {
be := mem.New() be := mem.New()
@ -263,7 +247,8 @@ func TestCheckerModifiedData(t *testing.T) {
OK(t, err) OK(t, err)
t.Logf("archived as %v", id.Str()) 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)) OK(t, checkRepo.SearchKey(TestPassword))
chkr := checker.New(checkRepo) chkr := checker.New(checkRepo)
@ -277,6 +262,7 @@ func TestCheckerModifiedData(t *testing.T) {
t.Errorf("expected no hints, got %v: %v", len(hints), hints) t.Errorf("expected no hints, got %v: %v", len(hints), hints)
} }
beError.ProduceErrors = true
errFound := false errFound := false
for _, err := range checkPacks(chkr) { for _, err := range checkPacks(chkr) {
t.Logf("pack error: %v", err) t.Logf("pack error: %v", err)