2
2
mirror of https://github.com/octoleo/restic.git synced 2024-05-28 22:50:48 +00:00

debug: Reduce code duplication

This commit is contained in:
Michael Eischer 2022-02-19 21:59:02 +01:00
parent 66f9048bce
commit fda7bb0f09

View File

@ -333,44 +333,37 @@ func loadBlobs(ctx context.Context, repo restic.Repository, pack restic.ID, list
nonce, plaintext := buf[:key.NonceSize()], buf[key.NonceSize():] nonce, plaintext := buf[:key.NonceSize()], buf[key.NonceSize():]
plaintext, err = key.Open(plaintext[:0], nonce, plaintext, nil) plaintext, err = key.Open(plaintext[:0], nonce, plaintext, nil)
outputPrefix := ""
filePrefix := ""
if err != nil { if err != nil {
Warnf("error decrypting blob: %v\n", err) Warnf("error decrypting blob: %v\n", err)
var plain []byte
if tryRepair || repairByte { if tryRepair || repairByte {
plain = tryRepairWithBitflip(ctx, key, buf, repairByte) plaintext = tryRepairWithBitflip(ctx, key, buf, repairByte)
} }
var prefix string if plaintext != nil {
if plain != nil { outputPrefix = "repaired "
id := restic.Hash(plain) filePrefix = "repaired-"
if !id.Equal(blob.ID) {
Printf(" repaired blob (length %v), hash is %v, ID does not match, wanted %v\n", len(plain), id, blob.ID)
prefix = "repaired-wrong-hash-"
} else {
Printf(" successfully repaired blob (length %v), hash is %v, ID matches\n", len(plain), id)
prefix = "repaired-"
}
} else { } else {
plain = decryptUnsigned(ctx, key, buf) plaintext = decryptUnsigned(ctx, key, buf)
prefix = "damaged-" err = storePlainBlob(blob.ID, "damaged-", plaintext)
if err != nil {
return err
}
continue
} }
err = storePlainBlob(blob.ID, prefix, plain)
if err != nil {
return err
}
continue
} }
id := restic.Hash(plaintext) id := restic.Hash(plaintext)
var prefix string var prefix string
if !id.Equal(blob.ID) { if !id.Equal(blob.ID) {
Printf(" successfully decrypted blob (length %v), hash is %v, ID does not match, wanted %v\n", len(plaintext), id, blob.ID) Printf(" successfully %vdecrypted blob (length %v), hash is %v, ID does not match, wanted %v\n", outputPrefix, len(plaintext), id, blob.ID)
prefix = "wrong-hash-" prefix = "wrong-hash-"
} else { } else {
Printf(" successfully decrypted blob (length %v), hash is %v, ID matches\n", len(plaintext), id) Printf(" successfully %vdecrypted blob (length %v), hash is %v, ID matches\n", outputPrefix, len(plaintext), id)
prefix = "correct-" prefix = "correct-"
} }
if extractPack { if extractPack {
err = storePlainBlob(id, prefix, plaintext) err = storePlainBlob(id, filePrefix+prefix, plaintext)
if err != nil { if err != nil {
return err return err
} }
@ -476,27 +469,15 @@ func examinePack(ctx context.Context, repo restic.Repository, id restic.ID) erro
blobsLoaded := false blobsLoaded := false
// examine all data the indexes have for the pack file // examine all data the indexes have for the pack file
for _, idx := range repo.Index().(*repository.MasterIndex).All() { for b := range repo.Index().ListPacks(ctx, restic.NewIDSet(id)) {
idxIDs, err := idx.IDs() blobs := b.Blobs
if err != nil {
idxIDs = restic.IDs{}
}
blobs := idx.ListPack(id)
if len(blobs) == 0 { if len(blobs) == 0 {
continue continue
} }
Printf(" index %v:\n", idxIDs) checkPackSize(blobs, fi.Size)
// convert list of blobs to []restic.Blob err = loadBlobs(ctx, repo, id, blobs)
var list []restic.Blob
for _, b := range blobs {
list = append(list, b.Blob)
}
checkPackSize(list, fi.Size)
err = loadBlobs(ctx, repo, id, list)
if err != nil { if err != nil {
Warnf("error: %v\n", err) Warnf("error: %v\n", err)
} else { } else {
@ -532,14 +513,10 @@ func checkPackSize(blobs []restic.Blob, fileSize int64) {
if offset != uint64(pb.Offset) { if offset != uint64(pb.Offset) {
Printf(" hole in file, want offset %v, got %v\n", offset, pb.Offset) Printf(" hole in file, want offset %v, got %v\n", offset, pb.Offset)
} }
offset += uint64(pb.Length) offset = uint64(pb.Offset + pb.Length)
size += uint64(pb.Length) size += uint64(pb.Length)
} }
size += uint64(pack.CalculateHeaderSize(blobs))
// compute header size, per blob: 1 byte type, 4 byte length, 32 byte id
size += uint64(restic.CiphertextLength(len(blobs) * (1 + 4 + 32)))
// length in uint32 little endian
size += 4
if uint64(fileSize) != size { if uint64(fileSize) != size {
Printf(" file sizes do not match: computed %v from index, file size is %v\n", size, fileSize) Printf(" file sizes do not match: computed %v from index, file size is %v\n", size, fileSize)