2
2
mirror of https://github.com/octoleo/restic.git synced 2024-12-26 20:30:19 +00:00

restorer: minor code cleanups

This commit is contained in:
Michael Eischer 2024-07-20 12:32:08 +02:00
parent 5d6ce59a8d
commit 6f8e17a463

View File

@ -93,17 +93,20 @@ func (r *fileRestorer) targetPath(location string) string {
return filepath.Join(r.dst, location) return filepath.Join(r.dst, location)
} }
func (r *fileRestorer) forEachBlob(blobIDs []restic.ID, fn func(packID restic.ID, packBlob restic.Blob, idx int)) error { func (r *fileRestorer) forEachBlob(blobIDs []restic.ID, fn func(packID restic.ID, packBlob restic.Blob, idx int, fileOffset int64)) error {
if len(blobIDs) == 0 { if len(blobIDs) == 0 {
return nil return nil
} }
fileOffset := int64(0)
for i, blobID := range blobIDs { for i, blobID := range blobIDs {
packs := r.idx(restic.DataBlob, blobID) packs := r.idx(restic.DataBlob, blobID)
if len(packs) == 0 { if len(packs) == 0 {
return errors.Errorf("Unknown blob %s", blobID.String()) return errors.Errorf("Unknown blob %s", blobID.String())
} }
fn(packs[0].PackID, packs[0].Blob, i) pb := packs[0]
fn(pb.PackID, pb.Blob, i, fileOffset)
fileOffset += int64(pb.DataLength())
} }
return nil return nil
@ -124,10 +127,10 @@ func (r *fileRestorer) restoreFiles(ctx context.Context) error {
var packsMap map[restic.ID][]fileBlobInfo var packsMap map[restic.ID][]fileBlobInfo
if largeFile { if largeFile {
packsMap = make(map[restic.ID][]fileBlobInfo) packsMap = make(map[restic.ID][]fileBlobInfo)
file.blobs = packsMap
} }
fileOffset := int64(0)
restoredBlobs := false restoredBlobs := false
err := r.forEachBlob(fileBlobs, func(packID restic.ID, blob restic.Blob, idx int) { err := r.forEachBlob(fileBlobs, func(packID restic.ID, blob restic.Blob, idx int, fileOffset int64) {
if !file.state.HasMatchingBlob(idx) { if !file.state.HasMatchingBlob(idx) {
if largeFile { if largeFile {
packsMap[packID] = append(packsMap[packID], fileBlobInfo{id: blob.ID, offset: fileOffset}) packsMap[packID] = append(packsMap[packID], fileBlobInfo{id: blob.ID, offset: fileOffset})
@ -136,7 +139,6 @@ func (r *fileRestorer) restoreFiles(ctx context.Context) error {
} else { } else {
r.reportBlobProgress(file, uint64(blob.DataLength())) r.reportBlobProgress(file, uint64(blob.DataLength()))
} }
fileOffset += int64(blob.DataLength())
pack, ok := packs[packID] pack, ok := packs[packID]
if !ok { if !ok {
pack = &packInfo{ pack = &packInfo{
@ -151,6 +153,11 @@ func (r *fileRestorer) restoreFiles(ctx context.Context) error {
file.sparse = r.sparse file.sparse = r.sparse
} }
}) })
if err != nil {
// repository index is messed up, can't do anything
return err
}
if len(fileBlobs) == 1 { if len(fileBlobs) == 1 {
// no need to preallocate files with a single block, thus we can always consider them to be sparse // no need to preallocate files with a single block, thus we can always consider them to be sparse
// in addition, a short chunk will never match r.zeroChunk which would prevent sparseness for short files // in addition, a short chunk will never match r.zeroChunk which would prevent sparseness for short files
@ -163,14 +170,6 @@ func (r *fileRestorer) restoreFiles(ctx context.Context) error {
file.sparse = false file.sparse = false
} }
if err != nil {
// repository index is messed up, can't do anything
return err
}
if largeFile {
file.blobs = packsMap
}
// empty file or one with already uptodate content. Make sure that the file size is correct // empty file or one with already uptodate content. Make sure that the file size is correct
if !restoredBlobs { if !restoredBlobs {
err := r.truncateFileToSize(file.location, file.size) err := r.truncateFileToSize(file.location, file.size)
@ -249,12 +248,10 @@ func (r *fileRestorer) downloadPack(ctx context.Context, pack *packInfo) error {
blobInfo.files[file] = append(blobInfo.files[file], fileOffset) blobInfo.files[file] = append(blobInfo.files[file], fileOffset)
} }
if fileBlobs, ok := file.blobs.(restic.IDs); ok { if fileBlobs, ok := file.blobs.(restic.IDs); ok {
fileOffset := int64(0) err := r.forEachBlob(fileBlobs, func(packID restic.ID, blob restic.Blob, idx int, fileOffset int64) {
err := r.forEachBlob(fileBlobs, func(packID restic.ID, blob restic.Blob, idx int) {
if packID.Equal(pack.id) && !file.state.HasMatchingBlob(idx) { if packID.Equal(pack.id) && !file.state.HasMatchingBlob(idx) {
addBlob(blob, fileOffset) addBlob(blob, fileOffset)
} }
fileOffset += int64(blob.DataLength())
}) })
if err != nil { if err != nil {
// restoreFiles should have caught this error before // restoreFiles should have caught this error before