2022-03-28 20:23:47 +00:00
|
|
|
//go:build debug
|
2017-10-12 18:14:48 +00:00
|
|
|
// +build debug
|
2015-05-16 12:24:24 +00:00
|
|
|
|
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2017-06-05 22:37:25 +00:00
|
|
|
"context"
|
2018-10-03 09:46:58 +00:00
|
|
|
"crypto/aes"
|
|
|
|
"crypto/cipher"
|
2015-05-16 12:24:24 +00:00
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
|
|
|
"io"
|
2018-09-28 12:30:43 +00:00
|
|
|
"os"
|
2018-09-29 12:16:01 +00:00
|
|
|
"runtime"
|
2018-09-28 12:30:43 +00:00
|
|
|
"sort"
|
2022-10-15 15:25:45 +00:00
|
|
|
"sync"
|
2018-09-29 12:16:01 +00:00
|
|
|
"time"
|
2015-05-16 12:24:24 +00:00
|
|
|
|
2022-02-19 20:59:47 +00:00
|
|
|
"github.com/klauspost/compress/zstd"
|
2016-09-17 10:36:05 +00:00
|
|
|
"github.com/spf13/cobra"
|
2018-09-29 12:16:01 +00:00
|
|
|
"golang.org/x/sync/errgroup"
|
2016-09-17 10:36:05 +00:00
|
|
|
|
2018-09-29 12:40:48 +00:00
|
|
|
"github.com/restic/restic/internal/backend"
|
2018-09-29 12:16:01 +00:00
|
|
|
"github.com/restic/restic/internal/crypto"
|
2017-07-23 12:21:03 +00:00
|
|
|
"github.com/restic/restic/internal/errors"
|
2022-06-12 12:43:43 +00:00
|
|
|
"github.com/restic/restic/internal/index"
|
2017-07-23 12:21:03 +00:00
|
|
|
"github.com/restic/restic/internal/pack"
|
|
|
|
"github.com/restic/restic/internal/repository"
|
2017-07-24 15:42:25 +00:00
|
|
|
"github.com/restic/restic/internal/restic"
|
2015-05-16 12:24:24 +00:00
|
|
|
)
|
|
|
|
|
2017-10-12 18:18:45 +00:00
|
|
|
var cmdDebug = &cobra.Command{
|
|
|
|
Use: "debug",
|
|
|
|
Short: "Debug commands",
|
|
|
|
}
|
|
|
|
|
2017-10-14 11:55:00 +00:00
|
|
|
var cmdDebugDump = &cobra.Command{
|
2017-10-01 15:04:52 +00:00
|
|
|
Use: "dump [indexes|snapshots|all|packs]",
|
2017-09-11 16:32:44 +00:00
|
|
|
Short: "Dump data structures",
|
2016-09-17 10:36:05 +00:00
|
|
|
Long: `
|
2017-02-13 15:05:25 +00:00
|
|
|
The "dump" command dumps data structures from the repository as JSON objects. It
|
2019-11-05 06:03:38 +00:00
|
|
|
is used for debugging purposes only.
|
|
|
|
|
|
|
|
EXIT STATUS
|
|
|
|
===========
|
|
|
|
|
|
|
|
Exit status is 0 if the command was successful, and non-zero if there was any error.
|
|
|
|
`,
|
2017-08-06 19:02:16 +00:00
|
|
|
DisableAutoGenTag: true,
|
2016-09-17 10:36:05 +00:00
|
|
|
RunE: func(cmd *cobra.Command, args []string) error {
|
2022-10-02 21:24:37 +00:00
|
|
|
return runDebugDump(cmd.Context(), globalOptions, args)
|
2016-09-17 10:36:05 +00:00
|
|
|
},
|
2015-06-21 11:02:56 +00:00
|
|
|
}
|
2015-05-16 12:24:24 +00:00
|
|
|
|
2024-01-21 16:43:13 +00:00
|
|
|
type DebugExamineOptions struct {
|
|
|
|
TryRepair bool
|
|
|
|
RepairByte bool
|
|
|
|
ExtractPack bool
|
|
|
|
ReuploadBlobs bool
|
|
|
|
}
|
|
|
|
|
|
|
|
var debugExamineOpts DebugExamineOptions
|
2018-09-29 12:16:01 +00:00
|
|
|
|
2015-05-16 12:24:24 +00:00
|
|
|
func init() {
|
2017-10-12 18:18:45 +00:00
|
|
|
cmdRoot.AddCommand(cmdDebug)
|
2017-10-14 11:55:00 +00:00
|
|
|
cmdDebug.AddCommand(cmdDebugDump)
|
2018-09-28 12:30:43 +00:00
|
|
|
cmdDebug.AddCommand(cmdDebugExamine)
|
2024-01-21 16:43:13 +00:00
|
|
|
cmdDebugExamine.Flags().BoolVar(&debugExamineOpts.ExtractPack, "extract-pack", false, "write blobs to the current directory")
|
|
|
|
cmdDebugExamine.Flags().BoolVar(&debugExamineOpts.ReuploadBlobs, "reupload-blobs", false, "reupload blobs to the repository")
|
|
|
|
cmdDebugExamine.Flags().BoolVar(&debugExamineOpts.TryRepair, "try-repair", false, "try to repair broken blobs with single bit flips")
|
|
|
|
cmdDebugExamine.Flags().BoolVar(&debugExamineOpts.RepairByte, "repair-byte", false, "try to repair broken blobs by trying bytes")
|
2015-05-16 12:24:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func prettyPrintJSON(wr io.Writer, item interface{}) error {
|
|
|
|
buf, err := json.MarshalIndent(item, "", " ")
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err = wr.Write(append(buf, '\n'))
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2020-04-10 09:34:37 +00:00
|
|
|
func debugPrintSnapshots(ctx context.Context, repo *repository.Repository, wr io.Writer) error {
|
2023-10-01 11:05:56 +00:00
|
|
|
return restic.ForAllSnapshots(ctx, repo, repo, nil, func(id restic.ID, snapshot *restic.Snapshot, err error) error {
|
2015-05-16 12:24:24 +00:00
|
|
|
if err != nil {
|
2018-01-21 16:25:36 +00:00
|
|
|
return err
|
2015-05-16 12:24:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fmt.Fprintf(wr, "snapshot_id: %v\n", id)
|
|
|
|
|
2018-01-21 16:25:36 +00:00
|
|
|
return prettyPrintJSON(wr, snapshot)
|
|
|
|
})
|
2015-05-16 12:24:24 +00:00
|
|
|
}
|
|
|
|
|
2016-02-04 18:37:48 +00:00
|
|
|
// Pack is the struct used in printPacks.
|
|
|
|
type Pack struct {
|
|
|
|
Name string `json:"name"`
|
|
|
|
|
|
|
|
Blobs []Blob `json:"blobs"`
|
|
|
|
}
|
|
|
|
|
|
|
|
// Blob is the struct used in printPacks.
|
|
|
|
type Blob struct {
|
2016-09-01 14:04:29 +00:00
|
|
|
Type restic.BlobType `json:"type"`
|
|
|
|
Length uint `json:"length"`
|
|
|
|
ID restic.ID `json:"id"`
|
|
|
|
Offset uint `json:"offset"`
|
2016-02-04 18:37:48 +00:00
|
|
|
}
|
|
|
|
|
2020-04-10 09:34:37 +00:00
|
|
|
func printPacks(ctx context.Context, repo *repository.Repository, wr io.Writer) error {
|
2016-02-04 18:37:48 +00:00
|
|
|
|
2022-10-15 15:25:45 +00:00
|
|
|
var m sync.Mutex
|
2023-10-01 11:05:56 +00:00
|
|
|
return restic.ParallelList(ctx, repo, restic.PackFile, repo.Connections(), func(ctx context.Context, id restic.ID, size int64) error {
|
2022-06-12 12:47:29 +00:00
|
|
|
blobs, _, err := repo.ListPack(ctx, id, size)
|
2016-02-04 18:37:48 +00:00
|
|
|
if err != nil {
|
2020-04-04 17:41:24 +00:00
|
|
|
Warnf("error for pack %v: %v\n", id.Str(), err)
|
2018-01-21 16:25:36 +00:00
|
|
|
return nil
|
2016-02-04 18:37:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
p := Pack{
|
2018-01-21 16:25:36 +00:00
|
|
|
Name: id.String(),
|
|
|
|
Blobs: make([]Blob, len(blobs)),
|
2016-02-04 18:37:48 +00:00
|
|
|
}
|
2018-01-21 16:25:36 +00:00
|
|
|
for i, blob := range blobs {
|
2016-02-04 18:37:48 +00:00
|
|
|
p.Blobs[i] = Blob{
|
|
|
|
Type: blob.Type,
|
|
|
|
Length: blob.Length,
|
|
|
|
ID: blob.ID,
|
|
|
|
Offset: blob.Offset,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-10-15 15:25:45 +00:00
|
|
|
m.Lock()
|
|
|
|
defer m.Unlock()
|
2020-04-02 20:45:20 +00:00
|
|
|
return prettyPrintJSON(wr, p)
|
2018-01-21 16:25:36 +00:00
|
|
|
})
|
2016-02-04 18:37:48 +00:00
|
|
|
}
|
|
|
|
|
2024-01-19 21:44:50 +00:00
|
|
|
func dumpIndexes(ctx context.Context, repo restic.ListerLoaderUnpacked, wr io.Writer) error {
|
2023-10-01 11:05:56 +00:00
|
|
|
return index.ForAllIndexes(ctx, repo, repo, func(id restic.ID, idx *index.Index, oldFormat bool, err error) error {
|
2020-04-04 17:41:24 +00:00
|
|
|
Printf("index_id: %v\n", id)
|
2015-08-08 15:04:06 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2020-04-02 20:45:20 +00:00
|
|
|
return idx.Dump(wr)
|
2018-01-21 16:25:36 +00:00
|
|
|
})
|
2015-08-08 15:04:06 +00:00
|
|
|
}
|
|
|
|
|
2021-10-31 22:08:13 +00:00
|
|
|
func runDebugDump(ctx context.Context, gopts GlobalOptions, args []string) error {
|
2015-05-16 12:24:24 +00:00
|
|
|
if len(args) != 1 {
|
2017-02-10 18:39:49 +00:00
|
|
|
return errors.Fatal("type not specified")
|
2015-05-16 12:24:24 +00:00
|
|
|
}
|
|
|
|
|
2021-10-31 22:08:13 +00:00
|
|
|
repo, err := OpenRepository(ctx, gopts)
|
2015-05-16 12:24:24 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2016-09-17 10:36:05 +00:00
|
|
|
if !gopts.NoLock {
|
2021-10-31 22:19:27 +00:00
|
|
|
var lock *restic.Lock
|
2023-02-16 15:58:36 +00:00
|
|
|
lock, ctx, err = lockRepo(ctx, repo, gopts.RetryLock, gopts.JSON)
|
2016-09-17 10:36:05 +00:00
|
|
|
defer unlockRepo(lock)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2015-06-27 12:40:18 +00:00
|
|
|
}
|
|
|
|
|
2015-05-16 12:24:24 +00:00
|
|
|
tpe := args[0]
|
|
|
|
|
|
|
|
switch tpe {
|
2015-08-08 15:04:06 +00:00
|
|
|
case "indexes":
|
2023-05-07 20:21:56 +00:00
|
|
|
return dumpIndexes(ctx, repo, globalOptions.stdout)
|
2015-05-16 12:24:24 +00:00
|
|
|
case "snapshots":
|
2023-05-07 20:21:56 +00:00
|
|
|
return debugPrintSnapshots(ctx, repo, globalOptions.stdout)
|
2016-02-04 18:37:48 +00:00
|
|
|
case "packs":
|
2023-05-07 20:21:56 +00:00
|
|
|
return printPacks(ctx, repo, globalOptions.stdout)
|
2015-05-16 12:24:24 +00:00
|
|
|
case "all":
|
2020-04-04 17:41:24 +00:00
|
|
|
Printf("snapshots:\n")
|
2023-05-07 20:21:56 +00:00
|
|
|
err := debugPrintSnapshots(ctx, repo, globalOptions.stdout)
|
2015-05-16 12:24:24 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2020-04-04 17:41:24 +00:00
|
|
|
Printf("\nindexes:\n")
|
2023-05-07 20:21:56 +00:00
|
|
|
err = dumpIndexes(ctx, repo, globalOptions.stdout)
|
2015-08-08 15:04:06 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2015-05-16 12:24:24 +00:00
|
|
|
return nil
|
|
|
|
default:
|
2016-09-01 20:17:37 +00:00
|
|
|
return errors.Fatalf("no such type %q", tpe)
|
2015-05-16 12:24:24 +00:00
|
|
|
}
|
|
|
|
}
|
2018-09-28 12:30:43 +00:00
|
|
|
|
|
|
|
var cmdDebugExamine = &cobra.Command{
|
2021-03-10 21:22:33 +00:00
|
|
|
Use: "examine pack-ID...",
|
2018-09-28 12:30:43 +00:00
|
|
|
Short: "Examine a pack file",
|
|
|
|
DisableAutoGenTag: true,
|
|
|
|
RunE: func(cmd *cobra.Command, args []string) error {
|
2024-01-21 16:43:13 +00:00
|
|
|
return runDebugExamine(cmd.Context(), globalOptions, debugExamineOpts, args)
|
2018-09-28 12:30:43 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2020-10-09 12:33:07 +00:00
|
|
|
func tryRepairWithBitflip(ctx context.Context, key *crypto.Key, input []byte, bytewise bool) []byte {
|
2018-09-29 16:28:39 +00:00
|
|
|
if bytewise {
|
2021-03-10 20:20:21 +00:00
|
|
|
Printf(" trying to repair blob by finding a broken byte\n")
|
2018-09-29 16:28:39 +00:00
|
|
|
} else {
|
2021-03-10 20:20:21 +00:00
|
|
|
Printf(" trying to repair blob with single bit flip\n")
|
2018-09-29 16:28:39 +00:00
|
|
|
}
|
2018-09-29 12:16:01 +00:00
|
|
|
|
|
|
|
ch := make(chan int)
|
|
|
|
var wg errgroup.Group
|
|
|
|
done := make(chan struct{})
|
2020-10-09 12:33:07 +00:00
|
|
|
var fixed []byte
|
|
|
|
var found bool
|
2018-09-29 12:16:01 +00:00
|
|
|
|
2020-10-09 12:33:07 +00:00
|
|
|
workers := runtime.GOMAXPROCS(0)
|
2021-03-10 20:20:21 +00:00
|
|
|
Printf(" spinning up %d worker functions\n", runtime.GOMAXPROCS(0))
|
2020-10-09 12:33:07 +00:00
|
|
|
for i := 0; i < workers; i++ {
|
2018-09-29 12:16:01 +00:00
|
|
|
wg.Go(func() error {
|
|
|
|
// make a local copy of the buffer
|
|
|
|
buf := make([]byte, len(input))
|
|
|
|
copy(buf, input)
|
|
|
|
|
2021-03-10 20:07:52 +00:00
|
|
|
testFlip := func(idx int, pattern byte) bool {
|
|
|
|
// flip bits
|
|
|
|
buf[idx] ^= pattern
|
|
|
|
|
|
|
|
nonce, plaintext := buf[:key.NonceSize()], buf[key.NonceSize():]
|
|
|
|
plaintext, err := key.Open(plaintext[:0], nonce, plaintext, nil)
|
|
|
|
if err == nil {
|
2021-03-10 20:20:21 +00:00
|
|
|
Printf("\n")
|
|
|
|
Printf(" blob could be repaired by XORing byte %v with 0x%02x\n", idx, pattern)
|
|
|
|
Printf(" hash is %v\n", restic.Hash(plaintext))
|
2021-03-10 20:07:52 +00:00
|
|
|
close(done)
|
|
|
|
found = true
|
|
|
|
fixed = plaintext
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
// flip bits back
|
|
|
|
buf[idx] ^= pattern
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
for i := range ch {
|
|
|
|
if bytewise {
|
|
|
|
for j := 0; j < 255; j++ {
|
|
|
|
if testFlip(i, byte(j)) {
|
|
|
|
return nil
|
2018-09-29 16:28:39 +00:00
|
|
|
}
|
2021-03-10 20:07:52 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
for j := 0; j < 7; j++ {
|
|
|
|
// flip each bit once
|
|
|
|
if testFlip(i, (1 << uint(j))) {
|
|
|
|
return nil
|
2018-09-29 12:16:01 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-03-10 20:07:52 +00:00
|
|
|
return nil
|
2018-09-29 12:16:01 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2021-03-10 20:07:52 +00:00
|
|
|
wg.Go(func() error {
|
|
|
|
defer close(ch)
|
|
|
|
|
|
|
|
start := time.Now()
|
|
|
|
info := time.Now()
|
|
|
|
for i := range input {
|
|
|
|
select {
|
|
|
|
case ch <- i:
|
|
|
|
case <-done:
|
2021-03-10 20:20:21 +00:00
|
|
|
Printf(" done after %v\n", time.Since(start))
|
2021-03-10 20:07:52 +00:00
|
|
|
return nil
|
|
|
|
}
|
2018-09-29 12:16:01 +00:00
|
|
|
|
2021-03-10 20:07:52 +00:00
|
|
|
if time.Since(info) > time.Second {
|
|
|
|
secs := time.Since(start).Seconds()
|
|
|
|
gps := float64(i) / secs
|
|
|
|
remaining := len(input) - i
|
|
|
|
eta := time.Duration(float64(remaining)/gps) * time.Second
|
2018-09-29 12:16:01 +00:00
|
|
|
|
2021-03-10 20:20:21 +00:00
|
|
|
Printf("\r%d byte of %d done (%.2f%%), %.0f byte per second, ETA %v",
|
2021-03-10 20:07:52 +00:00
|
|
|
i, len(input), float32(i)/float32(len(input))*100, gps, eta)
|
|
|
|
info = time.Now()
|
|
|
|
}
|
2018-09-29 12:16:01 +00:00
|
|
|
}
|
2021-03-10 20:07:52 +00:00
|
|
|
return nil
|
|
|
|
})
|
|
|
|
err := wg.Wait()
|
|
|
|
if err != nil {
|
2023-12-06 12:11:55 +00:00
|
|
|
panic("all go routines can only return nil")
|
2018-09-29 12:16:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if !found {
|
2021-03-10 20:20:21 +00:00
|
|
|
Printf("\n blob could not be repaired\n")
|
2018-09-29 12:16:01 +00:00
|
|
|
}
|
2020-10-09 12:33:07 +00:00
|
|
|
return fixed
|
2018-10-03 09:46:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func decryptUnsigned(ctx context.Context, k *crypto.Key, buf []byte) []byte {
|
|
|
|
// strip signature at the end
|
|
|
|
l := len(buf)
|
|
|
|
nonce, ct := buf[:16], buf[16:l-16]
|
2020-10-09 12:33:07 +00:00
|
|
|
out := make([]byte, len(ct))
|
2018-10-03 09:46:58 +00:00
|
|
|
|
|
|
|
c, err := aes.NewCipher(k.EncryptionKey[:])
|
|
|
|
if err != nil {
|
|
|
|
panic(fmt.Sprintf("unable to create cipher: %v", err))
|
|
|
|
}
|
|
|
|
e := cipher.NewCTR(c, nonce)
|
|
|
|
e.XORKeyStream(out, ct)
|
|
|
|
|
2020-10-09 12:33:07 +00:00
|
|
|
return out
|
2018-10-03 09:46:58 +00:00
|
|
|
}
|
|
|
|
|
2024-01-21 16:43:13 +00:00
|
|
|
func loadBlobs(ctx context.Context, opts DebugExamineOptions, repo restic.Repository, packID restic.ID, list []restic.Blob) error {
|
2022-02-19 20:59:47 +00:00
|
|
|
dec, err := zstd.NewReader(nil)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
2018-09-28 12:30:43 +00:00
|
|
|
be := repo.Backend()
|
2023-10-01 09:40:12 +00:00
|
|
|
h := backend.Handle{
|
2022-06-12 12:47:29 +00:00
|
|
|
Name: packID.String(),
|
2021-03-10 20:07:52 +00:00
|
|
|
Type: restic.PackFile,
|
|
|
|
}
|
2018-09-28 12:30:43 +00:00
|
|
|
|
2022-11-09 21:13:17 +00:00
|
|
|
wg, ctx := errgroup.WithContext(ctx)
|
2018-09-28 12:30:43 +00:00
|
|
|
|
2024-01-21 16:43:13 +00:00
|
|
|
if opts.ReuploadBlobs {
|
2022-11-09 21:13:17 +00:00
|
|
|
repo.StartPackUploader(ctx, wg)
|
|
|
|
}
|
|
|
|
|
|
|
|
wg.Go(func() error {
|
|
|
|
for _, blob := range list {
|
|
|
|
Printf(" loading blob %v at %v (length %v)\n", blob.ID, blob.Offset, blob.Length)
|
|
|
|
buf := make([]byte, blob.Length)
|
|
|
|
err := be.Load(ctx, h, int(blob.Length), int64(blob.Offset), func(rd io.Reader) error {
|
|
|
|
n, err := io.ReadFull(rd, buf)
|
2022-02-19 20:59:02 +00:00
|
|
|
if err != nil {
|
2022-11-09 21:13:17 +00:00
|
|
|
return fmt.Errorf("read error after %d bytes: %v", n, err)
|
2022-02-19 20:59:02 +00:00
|
|
|
}
|
2022-11-09 21:13:17 +00:00
|
|
|
return nil
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
Warnf("error read: %v\n", err)
|
2022-02-19 20:59:02 +00:00
|
|
|
continue
|
2018-10-03 09:46:58 +00:00
|
|
|
}
|
2018-09-28 12:30:43 +00:00
|
|
|
|
2022-11-09 21:13:17 +00:00
|
|
|
key := repo.Key()
|
|
|
|
|
|
|
|
nonce, plaintext := buf[:key.NonceSize()], buf[key.NonceSize():]
|
|
|
|
plaintext, err = key.Open(plaintext[:0], nonce, plaintext, nil)
|
|
|
|
outputPrefix := ""
|
|
|
|
filePrefix := ""
|
2022-02-19 20:59:47 +00:00
|
|
|
if err != nil {
|
2022-11-09 21:13:17 +00:00
|
|
|
Warnf("error decrypting blob: %v\n", err)
|
2024-01-21 16:43:13 +00:00
|
|
|
if opts.TryRepair || opts.RepairByte {
|
|
|
|
plaintext = tryRepairWithBitflip(ctx, key, buf, opts.RepairByte)
|
2022-11-09 21:13:17 +00:00
|
|
|
}
|
|
|
|
if plaintext != nil {
|
|
|
|
outputPrefix = "repaired "
|
|
|
|
filePrefix = "repaired-"
|
|
|
|
} else {
|
|
|
|
plaintext = decryptUnsigned(ctx, key, buf)
|
|
|
|
err = storePlainBlob(blob.ID, "damaged-", plaintext)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
continue
|
|
|
|
}
|
2022-02-19 20:59:47 +00:00
|
|
|
}
|
2022-11-09 21:13:17 +00:00
|
|
|
|
|
|
|
if blob.IsCompressed() {
|
|
|
|
decompressed, err := dec.DecodeAll(plaintext, nil)
|
|
|
|
if err != nil {
|
|
|
|
Printf(" failed to decompress blob %v\n", blob.ID)
|
|
|
|
}
|
|
|
|
if decompressed != nil {
|
|
|
|
plaintext = decompressed
|
|
|
|
}
|
2022-02-19 20:59:47 +00:00
|
|
|
}
|
|
|
|
|
2022-11-09 21:13:17 +00:00
|
|
|
id := restic.Hash(plaintext)
|
|
|
|
var prefix string
|
|
|
|
if !id.Equal(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-"
|
|
|
|
} else {
|
|
|
|
Printf(" successfully %vdecrypted blob (length %v), hash is %v, ID matches\n", outputPrefix, len(plaintext), id)
|
|
|
|
prefix = "correct-"
|
2020-10-09 12:33:07 +00:00
|
|
|
}
|
2024-01-21 16:43:13 +00:00
|
|
|
if opts.ExtractPack {
|
2022-11-09 21:13:17 +00:00
|
|
|
err = storePlainBlob(id, filePrefix+prefix, plaintext)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
2024-01-21 16:43:13 +00:00
|
|
|
if opts.ReuploadBlobs {
|
2022-11-09 21:13:17 +00:00
|
|
|
_, _, _, err := repo.SaveBlob(ctx, blob.Type, plaintext, id, true)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
Printf(" uploaded %v %v\n", blob.Type, id)
|
2022-04-10 17:35:24 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-01-21 16:43:13 +00:00
|
|
|
if opts.ReuploadBlobs {
|
2022-11-09 21:13:17 +00:00
|
|
|
return repo.Flush(ctx)
|
2022-04-10 17:35:24 +00:00
|
|
|
}
|
2022-11-09 21:13:17 +00:00
|
|
|
return nil
|
|
|
|
})
|
2018-09-28 12:30:43 +00:00
|
|
|
|
2022-11-09 21:13:17 +00:00
|
|
|
return wg.Wait()
|
2018-09-28 12:30:43 +00:00
|
|
|
}
|
|
|
|
|
2020-10-09 12:33:07 +00:00
|
|
|
func storePlainBlob(id restic.ID, prefix string, plain []byte) error {
|
|
|
|
filename := fmt.Sprintf("%s%s.bin", prefix, id)
|
|
|
|
f, err := os.Create(filename)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err = f.Write(plain)
|
|
|
|
if err != nil {
|
|
|
|
_ = f.Close()
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
err = f.Close()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2021-03-10 20:20:21 +00:00
|
|
|
Printf("decrypt of blob %v stored at %v\n", id, filename)
|
2020-10-09 12:33:07 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2024-01-21 16:43:13 +00:00
|
|
|
func runDebugExamine(ctx context.Context, gopts GlobalOptions, opts DebugExamineOptions, args []string) error {
|
2021-10-31 22:08:13 +00:00
|
|
|
repo, err := OpenRepository(ctx, gopts)
|
2022-04-30 18:03:26 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2021-03-10 19:41:11 +00:00
|
|
|
ids := make([]restic.ID, 0)
|
|
|
|
for _, name := range args {
|
|
|
|
id, err := restic.ParseID(name)
|
|
|
|
if err != nil {
|
2023-10-01 11:05:56 +00:00
|
|
|
id, err = restic.Find(ctx, repo, restic.PackFile, name)
|
2022-04-30 18:03:26 +00:00
|
|
|
if err != nil {
|
|
|
|
Warnf("error: %v\n", err)
|
|
|
|
continue
|
|
|
|
}
|
2021-03-10 19:41:11 +00:00
|
|
|
}
|
|
|
|
ids = append(ids, id)
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(ids) == 0 {
|
|
|
|
return errors.Fatal("no pack files to examine")
|
|
|
|
}
|
|
|
|
|
2018-09-28 12:30:43 +00:00
|
|
|
if !gopts.NoLock {
|
2021-10-31 22:19:27 +00:00
|
|
|
var lock *restic.Lock
|
2023-02-16 15:58:36 +00:00
|
|
|
lock, ctx, err = lockRepo(ctx, repo, gopts.RetryLock, gopts.JSON)
|
2018-09-28 12:30:43 +00:00
|
|
|
defer unlockRepo(lock)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-10-01 17:38:09 +00:00
|
|
|
bar := newIndexProgress(gopts.Quiet, gopts.JSON)
|
2023-07-16 02:48:30 +00:00
|
|
|
err = repo.LoadIndex(ctx, bar)
|
2018-09-28 12:30:43 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2021-03-10 19:41:11 +00:00
|
|
|
for _, id := range ids {
|
2024-01-21 16:43:13 +00:00
|
|
|
err := examinePack(ctx, opts, repo, id)
|
2021-03-10 19:41:11 +00:00
|
|
|
if err != nil {
|
2021-03-10 20:20:21 +00:00
|
|
|
Warnf("error: %v\n", err)
|
2021-03-10 19:41:11 +00:00
|
|
|
}
|
|
|
|
if err == context.Canceled {
|
|
|
|
break
|
|
|
|
}
|
2021-03-10 19:21:05 +00:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
2018-09-28 12:30:43 +00:00
|
|
|
|
2024-01-21 16:43:13 +00:00
|
|
|
func examinePack(ctx context.Context, opts DebugExamineOptions, repo restic.Repository, id restic.ID) error {
|
2021-03-10 20:20:21 +00:00
|
|
|
Printf("examine %v\n", id)
|
2018-09-28 12:30:43 +00:00
|
|
|
|
2023-10-01 09:40:12 +00:00
|
|
|
h := backend.Handle{
|
2021-03-10 19:21:05 +00:00
|
|
|
Type: restic.PackFile,
|
2021-03-10 19:41:11 +00:00
|
|
|
Name: id.String(),
|
2021-03-10 19:21:05 +00:00
|
|
|
}
|
|
|
|
fi, err := repo.Backend().Stat(ctx, h)
|
|
|
|
if err != nil {
|
2021-03-10 19:41:11 +00:00
|
|
|
return err
|
2021-03-10 19:21:05 +00:00
|
|
|
}
|
2021-03-10 20:20:21 +00:00
|
|
|
Printf(" file size is %v\n", fi.Size)
|
2018-09-28 12:30:43 +00:00
|
|
|
|
2021-03-10 19:21:05 +00:00
|
|
|
buf, err := backend.LoadAll(ctx, nil, repo.Backend(), h)
|
|
|
|
if err != nil {
|
2021-03-10 19:41:11 +00:00
|
|
|
return err
|
2021-03-10 19:21:05 +00:00
|
|
|
}
|
|
|
|
gotID := restic.Hash(buf)
|
|
|
|
if !id.Equal(gotID) {
|
2021-03-10 20:20:21 +00:00
|
|
|
Printf(" wanted hash %v, got %v\n", id, gotID)
|
2021-03-10 19:21:05 +00:00
|
|
|
} else {
|
2021-03-10 20:20:21 +00:00
|
|
|
Printf(" hash for file content matches\n")
|
2021-03-10 19:21:05 +00:00
|
|
|
}
|
2018-09-28 12:30:43 +00:00
|
|
|
|
2021-03-10 20:20:21 +00:00
|
|
|
Printf(" ========================================\n")
|
|
|
|
Printf(" looking for info in the indexes\n")
|
2018-09-29 12:16:01 +00:00
|
|
|
|
2021-03-10 19:41:11 +00:00
|
|
|
blobsLoaded := false
|
2021-03-10 19:21:05 +00:00
|
|
|
// examine all data the indexes have for the pack file
|
2022-02-19 20:59:02 +00:00
|
|
|
for b := range repo.Index().ListPacks(ctx, restic.NewIDSet(id)) {
|
|
|
|
blobs := b.Blobs
|
2021-03-10 19:21:05 +00:00
|
|
|
if len(blobs) == 0 {
|
|
|
|
continue
|
2018-09-28 12:30:43 +00:00
|
|
|
}
|
|
|
|
|
2022-02-19 20:59:02 +00:00
|
|
|
checkPackSize(blobs, fi.Size)
|
2021-03-10 19:21:05 +00:00
|
|
|
|
2024-01-21 16:43:13 +00:00
|
|
|
err = loadBlobs(ctx, opts, repo, id, blobs)
|
2021-03-10 19:21:05 +00:00
|
|
|
if err != nil {
|
2021-03-10 20:20:21 +00:00
|
|
|
Warnf("error: %v\n", err)
|
2021-03-10 19:21:05 +00:00
|
|
|
} else {
|
|
|
|
blobsLoaded = true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-10 20:20:21 +00:00
|
|
|
Printf(" ========================================\n")
|
|
|
|
Printf(" inspect the pack itself\n")
|
2021-03-10 19:21:05 +00:00
|
|
|
|
2022-06-12 12:47:29 +00:00
|
|
|
blobs, _, err := repo.ListPack(ctx, id, fi.Size)
|
2021-03-10 19:21:05 +00:00
|
|
|
if err != nil {
|
2021-03-10 19:41:11 +00:00
|
|
|
return fmt.Errorf("pack %v: %v", id.Str(), err)
|
2021-03-10 19:21:05 +00:00
|
|
|
}
|
2021-03-10 19:57:14 +00:00
|
|
|
checkPackSize(blobs, fi.Size)
|
2021-03-10 19:21:05 +00:00
|
|
|
|
2021-03-10 19:57:14 +00:00
|
|
|
if !blobsLoaded {
|
2024-01-21 16:43:13 +00:00
|
|
|
return loadBlobs(ctx, opts, repo, id, blobs)
|
2021-03-10 19:57:14 +00:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func checkPackSize(blobs []restic.Blob, fileSize int64) {
|
2021-03-10 19:21:05 +00:00
|
|
|
// track current size and offset
|
|
|
|
var size, offset uint64
|
|
|
|
|
|
|
|
sort.Slice(blobs, func(i, j int) bool {
|
|
|
|
return blobs[i].Offset < blobs[j].Offset
|
|
|
|
})
|
|
|
|
|
|
|
|
for _, pb := range blobs {
|
2021-03-10 20:20:21 +00:00
|
|
|
Printf(" %v blob %v, offset %-6d, raw length %-6d\n", pb.Type, pb.ID, pb.Offset, pb.Length)
|
2021-03-10 19:21:05 +00:00
|
|
|
if offset != uint64(pb.Offset) {
|
2021-03-10 20:20:21 +00:00
|
|
|
Printf(" hole in file, want offset %v, got %v\n", offset, pb.Offset)
|
2021-03-10 19:21:05 +00:00
|
|
|
}
|
2022-02-19 20:59:02 +00:00
|
|
|
offset = uint64(pb.Offset + pb.Length)
|
2021-03-10 19:21:05 +00:00
|
|
|
size += uint64(pb.Length)
|
|
|
|
}
|
2022-02-19 20:59:02 +00:00
|
|
|
size += uint64(pack.CalculateHeaderSize(blobs))
|
2021-03-10 19:21:05 +00:00
|
|
|
|
2021-03-10 19:57:14 +00:00
|
|
|
if uint64(fileSize) != size {
|
2022-06-12 12:47:29 +00:00
|
|
|
Printf(" file sizes do not match: computed %v, file size is %v\n", size, fileSize)
|
2021-03-10 19:21:05 +00:00
|
|
|
} else {
|
2021-03-10 20:20:21 +00:00
|
|
|
Printf(" file sizes match\n")
|
2021-03-10 19:21:05 +00:00
|
|
|
}
|
2018-09-28 12:30:43 +00:00
|
|
|
}
|