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"
"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
}

View File

@ -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)