Refactor index decoding

Decoding old-format indices no longer requires loading and decrypting
twice.
This commit is contained in:
greatroar committed 2020-10-13 20:47:50 +02:00
1 parent 6003dada14
commit 27db3ec262
4 files changed
+37 -54

No files matched your search

+7 -29
View File
@@ -522,11 +522,8 @@ func isErrOldIndex(err error) bool {
return false
}
// ErrOldIndexFormat means an index with the old format was detected.
var ErrOldIndexFormat = errors.New("index has old format")
// DecodeIndex loads and unserializes an index from rd.
func DecodeIndex(buf []byte) (idx *Index, err error) {
// DecodeIndex unserializes an index from buf.
func DecodeIndex(buf []byte) (idx *Index, oldFormat bool, err error) {
debug.Log("Start decoding index")
idxJSON := &jsonIndex{}
@@ -536,10 +533,11 @@ func DecodeIndex(buf []byte) (idx *Index, err error) {
if isErrOldIndex(err) {
debug.Log("index is probably old format, trying that")
err = ErrOldIndexFormat
idx, err = decodeOldIndex(buf)
return idx, err == nil, err
}
return nil, errors.Wrap(err, "Decode")
return nil, false, errors.Wrap(err, "DecodeIndex")
}
idx = NewIndex()
@@ -571,11 +569,11 @@ func DecodeIndex(buf []byte) (idx *Index, err error) {
idx.final = true
debug.Log("done")
return idx, nil
return idx, false, nil
}
// DecodeOldIndex loads and unserializes an index in the old format from rd.
func DecodeOldIndex(buf []byte) (idx *Index, err error) {
func decodeOldIndex(buf []byte) (idx *Index, err error) {
debug.Log("Start decoding old index")
list := []*packJSON{}
@@ -615,23 +613,3 @@ func DecodeOldIndex(buf []byte) (idx *Index, err error) {
debug.Log("done")
return idx, nil
}
// LoadIndexWithDecoder loads the index and decodes it with fn.
func LoadIndexWithDecoder(ctx context.Context, repo restic.Repository, buf []byte, id restic.ID, fn func([]byte) (*Index, error)) (*Index, []byte, error) {
debug.Log("Loading index %v", id)
buf, err := repo.LoadAndDecrypt(ctx, buf[:0], restic.IndexFile, id)
if err != nil {
return nil, buf[:0], err
}
idx, err := fn(buf)
if err != nil {
debug.Log("error while decoding index %v: %v", id, err)
return nil, buf[:0], err
}
idx.ids = append(idx.ids, id)
return idx, buf, nil
}