2
2
mirror of https://github.com/octoleo/restic.git synced 2024-11-24 21:57:41 +00:00

restore: cleanup downloadPack

This commit is contained in:
Michael Eischer 2024-01-07 12:06:36 +01:00
parent b856e9489a
commit cf352ccafb

View File

@ -199,18 +199,18 @@ func (r *fileRestorer) restoreFiles(ctx context.Context) error {
type blobToFileOffsetsMapping map[restic.ID]struct { type blobToFileOffsetsMapping map[restic.ID]struct {
files map[*fileInfo][]int64 // file -> offsets (plural!) of the blob in the file files map[*fileInfo][]int64 // file -> offsets (plural!) of the blob in the file
blob restic.Blob
} }
func (r *fileRestorer) downloadPack(ctx context.Context, pack *packInfo) error { func (r *fileRestorer) downloadPack(ctx context.Context, pack *packInfo) error {
// calculate blob->[]files->[]offsets mappings // calculate blob->[]files->[]offsets mappings
blobs := make(blobToFileOffsetsMapping) blobs := make(blobToFileOffsetsMapping)
var blobList []restic.Blob
for file := range pack.files { for file := range pack.files {
addBlob := func(blob restic.Blob, fileOffset int64) { addBlob := func(blob restic.Blob, fileOffset int64) {
blobInfo, ok := blobs[blob.ID] blobInfo, ok := blobs[blob.ID]
if !ok { if !ok {
blobInfo.files = make(map[*fileInfo][]int64) blobInfo.files = make(map[*fileInfo][]int64)
blobList = append(blobList, blob) blobInfo.blob = blob
blobs[blob.ID] = blobInfo blobs[blob.ID] = blobInfo
} }
blobInfo.files[file] = append(blobInfo.files[file], fileOffset) blobInfo.files[file] = append(blobInfo.files[file], fileOffset)
@ -242,17 +242,16 @@ func (r *fileRestorer) downloadPack(ctx context.Context, pack *packInfo) error {
// track already processed blobs for precise error reporting // track already processed blobs for precise error reporting
processedBlobs := restic.NewBlobSet() processedBlobs := restic.NewBlobSet()
err := r.downloadBlobs(ctx, pack.id, blobList, blobs, processedBlobs) err := r.downloadBlobs(ctx, pack.id, blobs, processedBlobs)
if err != nil { if err != nil {
// only report error for not yet processed blobs // only report error for not yet processed blobs
affectedFiles := make(map[*fileInfo]struct{}) affectedFiles := make(map[*fileInfo]struct{})
for _, blob := range blobList { for _, entry := range blobs {
if processedBlobs.Has(blob.BlobHandle) { if processedBlobs.Has(entry.blob.BlobHandle) {
continue continue
} }
blob := blobs[blob.ID] for file := range entry.files {
for file := range blob.files {
affectedFiles[file] = struct{}{} affectedFiles[file] = struct{}{}
} }
} }
@ -274,9 +273,13 @@ func (r *fileRestorer) sanitizeError(file *fileInfo, err error) error {
return err return err
} }
func (r *fileRestorer) downloadBlobs(ctx context.Context, packID restic.ID, blobList []restic.Blob, func (r *fileRestorer) downloadBlobs(ctx context.Context, packID restic.ID,
blobs blobToFileOffsetsMapping, processedBlobs restic.BlobSet) error { blobs blobToFileOffsetsMapping, processedBlobs restic.BlobSet) error {
blobList := make([]restic.Blob, 0, len(blobs))
for _, entry := range blobs {
blobList = append(blobList, entry.blob)
}
return repository.StreamPack(ctx, r.packLoader, r.key, packID, blobList, return repository.StreamPack(ctx, r.packLoader, r.key, packID, blobList,
func(h restic.BlobHandle, blobData []byte, err error) error { func(h restic.BlobHandle, blobData []byte, err error) error {
processedBlobs.Insert(h) processedBlobs.Insert(h)