Handle pack loading errors in rebuild-index

Errors returned from backend.LoadAll() were not handled, leading to
these fatal errors from the unpacker trying to read the size from the end of
an empty buffer:

`seeking to read header length failed: bytes.Reader.Seek: negative position`

This change takes care of returning on error, as well as showing which pack
failed to load and validating pack integrity.
This commit is contained in:
Rached Ben Mustapha 2016-02-06 16:03:14 +01:00
parent 0535490618
commit ba35ca522a
1 changed files with 11 additions and 0 deletions

View File

@ -2,6 +2,7 @@ package main
import (
"bytes"
"fmt"
"github.com/restic/restic/backend"
"github.com/restic/restic/debug"
@ -137,6 +138,16 @@ func (cmd CmdRebuildIndex) RebuildIndex() error {
h := backend.Handle{Type: backend.Data, Name: packID.String()}
buf, err = backend.LoadAll(cmd.repo.Backend(), h, buf)
if err != nil {
debug.Log("RebuildIndex.RebuildIndex", "error while loading pack %v", packID.Str())
return fmt.Errorf("error while loading pack %v: %v", packID.Str(), err)
}
hash := backend.Hash(buf)
if !hash.Equal(packID) {
debug.Log("RebuildIndex.RebuildIndex", "Pack ID does not match, want %v, got %v", packID.Str(), hash.Str())
return fmt.Errorf("Pack ID does not match, want %v, got %v", packID.Str(), hash.Str())
}
up, err := pack.NewUnpacker(cmd.repo.Key(), bytes.NewReader(buf))
if err != nil {