debug/repair packs/upgrade repo v2: use repository.LoadRaw

This replaces calling the low-level backend.Load() method.
This commit is contained in:
Michael Eischer 2024-05-09 19:01:20 +02:00
parent 1d6d3656b0
commit 779c8d3527
3 changed files with 17 additions and 32 deletions

View File

@ -316,10 +316,11 @@ func loadBlobs(ctx context.Context, opts DebugExamineOptions, repo restic.Reposi
if err != nil { if err != nil {
panic(err) panic(err)
} }
be := repo.Backend()
h := backend.Handle{ pack, err := repo.LoadRaw(ctx, restic.PackFile, packID)
Name: packID.String(), // allow processing broken pack files
Type: restic.PackFile, if pack == nil {
return err
} }
wg, ctx := errgroup.WithContext(ctx) wg, ctx := errgroup.WithContext(ctx)
@ -331,19 +332,11 @@ func loadBlobs(ctx context.Context, opts DebugExamineOptions, repo restic.Reposi
wg.Go(func() error { wg.Go(func() error {
for _, blob := range list { for _, blob := range list {
Printf(" loading blob %v at %v (length %v)\n", blob.ID, blob.Offset, blob.Length) Printf(" loading blob %v at %v (length %v)\n", blob.ID, blob.Offset, blob.Length)
buf := make([]byte, blob.Length) if int(blob.Offset+blob.Length) > len(pack) {
err := be.Load(ctx, h, int(blob.Length), int64(blob.Offset), func(rd io.Reader) error { Warnf("skipping truncated blob\n")
n, err := io.ReadFull(rd, buf)
if err != nil {
return fmt.Errorf("read error after %d bytes: %v", n, err)
}
return nil
})
if err != nil {
Warnf("error read: %v\n", err)
continue continue
} }
buf := pack[blob.Offset : blob.Offset+blob.Length]
key := repo.Key() key := repo.Key()
nonce, plaintext := buf[:key.NonceSize()], buf[key.NonceSize():] nonce, plaintext := buf[:key.NonceSize()], buf[key.NonceSize():]

View File

@ -1,11 +1,11 @@
package main package main
import ( import (
"bytes"
"context" "context"
"io" "io"
"os" "os"
"github.com/restic/restic/internal/backend"
"github.com/restic/restic/internal/errors" "github.com/restic/restic/internal/errors"
"github.com/restic/restic/internal/repository" "github.com/restic/restic/internal/repository"
"github.com/restic/restic/internal/restic" "github.com/restic/restic/internal/restic"
@ -68,20 +68,17 @@ func runRepairPacks(ctx context.Context, gopts GlobalOptions, term *termstatus.T
printer.P("saving backup copies of pack files to current folder") printer.P("saving backup copies of pack files to current folder")
for id := range ids { for id := range ids {
buf, err := repo.LoadRaw(ctx, restic.PackFile, id)
// corrupted data is fine
if buf == nil {
return err
}
f, err := os.OpenFile("pack-"+id.String(), os.O_WRONLY|os.O_CREATE|os.O_EXCL, 0o666) f, err := os.OpenFile("pack-"+id.String(), os.O_WRONLY|os.O_CREATE|os.O_EXCL, 0o666)
if err != nil { if err != nil {
return err return err
} }
if _, err := io.Copy(f, bytes.NewReader(buf)); err != nil {
err = repo.Backend().Load(ctx, backend.Handle{Type: restic.PackFile, Name: id.String()}, 0, 0, func(rd io.Reader) error {
_, err := f.Seek(0, 0)
if err != nil {
return err
}
_, err = io.Copy(f, rd)
return err
})
if err != nil {
_ = f.Close() _ = f.Close()
return err return err
} }

View File

@ -3,7 +3,6 @@ package migrations
import ( import (
"context" "context"
"fmt" "fmt"
"io"
"os" "os"
"path/filepath" "path/filepath"
@ -89,11 +88,7 @@ func (m *UpgradeRepoV2) Apply(ctx context.Context, repo restic.Repository) error
h := backend.Handle{Type: restic.ConfigFile} h := backend.Handle{Type: restic.ConfigFile}
// read raw config file and save it to a temp dir, just in case // read raw config file and save it to a temp dir, just in case
var rawConfigFile []byte rawConfigFile, err := repo.LoadRaw(ctx, restic.ConfigFile, restic.ID{})
err = repo.Backend().Load(ctx, h, 0, 0, func(rd io.Reader) (err error) {
rawConfigFile, err = io.ReadAll(rd)
return err
})
if err != nil { if err != nil {
return fmt.Errorf("load config file failed: %w", err) return fmt.Errorf("load config file failed: %w", err)
} }