Merge pull request #3025 from aawsome/fix-index-ids

Fix setting of ID in DecodeIndex
This commit is contained in:
MichaelEischer 2020-10-17 09:54:45 +02:00 committed by GitHub
commit 96a912b65a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 19 additions and 10 deletions

View File

@ -121,7 +121,7 @@ func (c *Checker) LoadIndex(ctx context.Context) (hints []error, errs []error) {
buf, err = c.repo.LoadAndDecrypt(ctx, buf[:0], restic.IndexFile, fi.ID)
if err == nil {
idx, oldFormat, err = repository.DecodeIndex(buf)
idx, oldFormat, err = repository.DecodeIndex(buf, fi.ID)
}
if oldFormat {

View File

@ -520,7 +520,7 @@ func isErrOldIndex(err error) bool {
}
// DecodeIndex unserializes an index from buf.
func DecodeIndex(buf []byte) (idx *Index, oldFormat bool, err error) {
func DecodeIndex(buf []byte, id restic.ID) (idx *Index, oldFormat bool, err error) {
debug.Log("Start decoding index")
idxJSON := &jsonIndex{}
@ -563,6 +563,7 @@ func DecodeIndex(buf []byte) (idx *Index, oldFormat bool, err error) {
}
}
idx.supersedes = idxJSON.Supersedes
idx.ids = append(idx.ids, id)
idx.final = true
debug.Log("done")

View File

@ -56,11 +56,15 @@ func TestIndexSerialize(t *testing.T) {
err := idx.Encode(wr)
rtest.OK(t, err)
idx2, oldFormat, err := repository.DecodeIndex(wr.Bytes())
idx2ID := restic.NewRandomID()
idx2, oldFormat, err := repository.DecodeIndex(wr.Bytes(), idx2ID)
rtest.OK(t, err)
rtest.Assert(t, idx2 != nil,
"nil returned for decoded index")
rtest.Assert(t, !oldFormat, "new index format recognized as old format")
indexID, err := idx2.IDs()
rtest.OK(t, err)
rtest.Equals(t, indexID, restic.IDs{idx2ID})
wr2 := bytes.NewBuffer(nil)
err = idx2.Encode(wr2)
@ -136,7 +140,7 @@ func TestIndexSerialize(t *testing.T) {
rtest.OK(t, err)
rtest.Equals(t, restic.IDs{id}, ids)
idx3, oldFormat, err := repository.DecodeIndex(wr3.Bytes())
idx3, oldFormat, err := repository.DecodeIndex(wr3.Bytes(), id)
rtest.OK(t, err)
rtest.Assert(t, idx3 != nil,
"nil returned for decoded index")
@ -287,7 +291,7 @@ var exampleLookupTest = struct {
func TestIndexUnserialize(t *testing.T) {
oldIdx := restic.IDs{restic.TestParseID("ed54ae36197f4745ebc4b54d10e0f623eaaaedd03013eb7ae90df881b7781452")}
idx, oldFormat, err := repository.DecodeIndex(docExample)
idx, oldFormat, err := repository.DecodeIndex(docExample, restic.NewRandomID())
rtest.OK(t, err)
rtest.Assert(t, !oldFormat, "new index format recognized as old format")
@ -338,28 +342,32 @@ func initBenchmarkIndexJSON() {
func BenchmarkDecodeIndex(b *testing.B) {
benchmarkIndexJSONOnce.Do(initBenchmarkIndexJSON)
id := restic.NewRandomID()
b.ResetTimer()
for i := 0; i < b.N; i++ {
_, _, err := repository.DecodeIndex(benchmarkIndexJSON)
_, _, err := repository.DecodeIndex(benchmarkIndexJSON, id)
rtest.OK(b, err)
}
}
func BenchmarkDecodeIndexParallel(b *testing.B) {
benchmarkIndexJSONOnce.Do(initBenchmarkIndexJSON)
id := restic.NewRandomID()
b.ResetTimer()
b.RunParallel(func(pb *testing.PB) {
for pb.Next() {
_, _, err := repository.DecodeIndex(benchmarkIndexJSON)
_, _, err := repository.DecodeIndex(benchmarkIndexJSON, id)
rtest.OK(b, err)
}
})
}
func TestIndexUnserializeOld(t *testing.T) {
idx, oldFormat, err := repository.DecodeIndex(docOldExample)
idx, oldFormat, err := repository.DecodeIndex(docOldExample, restic.NewRandomID())
rtest.OK(t, err)
rtest.Assert(t, oldFormat, "old index format recognized as new format")

View File

@ -461,7 +461,7 @@ func (r *Repository) LoadIndex(ctx context.Context) error {
if err != nil {
return errors.Wrapf(err, "unable to load index %s", fi.ID.Str())
}
idx, _, err := DecodeIndex(buf)
idx, _, err := DecodeIndex(buf, fi.ID)
if err != nil {
return errors.Wrapf(err, "unable to decode index %s", fi.ID.Str())
}
@ -580,7 +580,7 @@ func LoadIndex(ctx context.Context, repo restic.Repository, id restic.ID) (*Inde
return nil, err
}
idx, oldFormat, err := DecodeIndex(buf)
idx, oldFormat, err := DecodeIndex(buf, id)
if oldFormat {
fmt.Fprintf(os.Stderr, "index %v has old format\n", id.Str())
}