2024-04-06 17:12:04 +00:00
|
|
|
package repository
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
|
2024-05-24 21:06:44 +00:00
|
|
|
"github.com/restic/restic/internal/repository/index"
|
2024-05-24 21:09:58 +00:00
|
|
|
"github.com/restic/restic/internal/repository/pack"
|
2024-04-06 17:12:04 +00:00
|
|
|
"github.com/restic/restic/internal/restic"
|
|
|
|
"github.com/restic/restic/internal/ui/progress"
|
|
|
|
)
|
|
|
|
|
|
|
|
type RepairIndexOptions struct {
|
|
|
|
ReadAllPacks bool
|
|
|
|
}
|
|
|
|
|
|
|
|
func RepairIndex(ctx context.Context, repo *Repository, opts RepairIndexOptions, printer progress.Printer) error {
|
|
|
|
var obsoleteIndexes restic.IDs
|
|
|
|
packSizeFromList := make(map[restic.ID]int64)
|
|
|
|
packSizeFromIndex := make(map[restic.ID]int64)
|
|
|
|
removePacks := restic.NewIDSet()
|
|
|
|
|
|
|
|
if opts.ReadAllPacks {
|
|
|
|
// get list of old index files but start with empty index
|
|
|
|
err := repo.List(ctx, restic.IndexFile, func(id restic.ID, _ int64) error {
|
|
|
|
obsoleteIndexes = append(obsoleteIndexes, id)
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2024-05-19 13:55:42 +00:00
|
|
|
repo.clearIndex()
|
|
|
|
|
2024-04-06 17:12:04 +00:00
|
|
|
} else {
|
|
|
|
printer.P("loading indexes...\n")
|
|
|
|
mi := index.NewMasterIndex()
|
2024-08-26 18:07:21 +00:00
|
|
|
err := index.ForAllIndexes(ctx, repo, repo, func(id restic.ID, idx *index.Index, err error) error {
|
2024-04-06 17:12:04 +00:00
|
|
|
if err != nil {
|
|
|
|
printer.E("removing invalid index %v: %v\n", id, err)
|
|
|
|
obsoleteIndexes = append(obsoleteIndexes, id)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
mi.Insert(idx)
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
err = mi.MergeFinalIndexes()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
err = repo.SetIndex(mi)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2024-05-19 10:41:56 +00:00
|
|
|
packSizeFromIndex, err = pack.Size(ctx, repo, false)
|
2024-04-05 20:20:14 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2024-04-06 17:12:04 +00:00
|
|
|
}
|
|
|
|
|
2024-05-19 18:38:58 +00:00
|
|
|
oldIndexes := repo.idx.IDs()
|
|
|
|
|
2024-04-06 17:12:04 +00:00
|
|
|
printer.P("getting pack files to read...\n")
|
|
|
|
err := repo.List(ctx, restic.PackFile, func(id restic.ID, packSize int64) error {
|
|
|
|
size, ok := packSizeFromIndex[id]
|
|
|
|
if !ok || size != packSize {
|
|
|
|
// Pack was not referenced in index or size does not match
|
|
|
|
packSizeFromList[id] = packSize
|
|
|
|
removePacks.Insert(id)
|
|
|
|
}
|
|
|
|
if !ok {
|
|
|
|
printer.E("adding pack file to index %v\n", id)
|
|
|
|
} else if size != packSize {
|
|
|
|
printer.E("reindexing pack file %v with unexpected size %v instead of %v\n", id, packSize, size)
|
|
|
|
}
|
|
|
|
delete(packSizeFromIndex, id)
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
for id := range packSizeFromIndex {
|
|
|
|
// forget pack files that are referenced in the index but do not exist
|
|
|
|
// when rebuilding the index
|
|
|
|
removePacks.Insert(id)
|
|
|
|
printer.E("removing not found pack file %v\n", id)
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(packSizeFromList) > 0 {
|
|
|
|
printer.P("reading pack files\n")
|
|
|
|
bar := printer.NewCounter("packs")
|
|
|
|
bar.SetMax(uint64(len(packSizeFromList)))
|
2024-05-19 14:14:31 +00:00
|
|
|
invalidFiles, err := repo.createIndexFromPacks(ctx, packSizeFromList, bar)
|
2024-04-06 17:12:04 +00:00
|
|
|
bar.Done()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, id := range invalidFiles {
|
|
|
|
printer.V("skipped incomplete pack file: %v\n", id)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-05-19 18:38:58 +00:00
|
|
|
if err := repo.Flush(ctx); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
err = rewriteIndexFiles(ctx, repo, removePacks, oldIndexes, obsoleteIndexes, printer)
|
2024-04-14 11:46:21 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// drop outdated in-memory index
|
2024-05-19 10:51:58 +00:00
|
|
|
repo.clearIndex()
|
2024-04-14 11:46:21 +00:00
|
|
|
return nil
|
2024-04-06 17:12:04 +00:00
|
|
|
}
|
|
|
|
|
2024-05-19 18:38:58 +00:00
|
|
|
func rewriteIndexFiles(ctx context.Context, repo *Repository, removePacks restic.IDSet, oldIndexes restic.IDSet, extraObsolete restic.IDs, printer progress.Printer) error {
|
2024-04-06 17:12:04 +00:00
|
|
|
printer.P("rebuilding index\n")
|
|
|
|
|
2024-05-19 18:38:58 +00:00
|
|
|
bar := printer.NewCounter("indexes processed")
|
|
|
|
return repo.idx.Rewrite(ctx, repo, removePacks, oldIndexes, extraObsolete, index.MasterIndexRewriteOpts{
|
2024-04-06 17:12:04 +00:00
|
|
|
SaveProgress: bar,
|
|
|
|
DeleteProgress: func() *progress.Counter {
|
|
|
|
return printer.NewCounter("old indexes deleted")
|
|
|
|
},
|
2024-04-14 09:51:27 +00:00
|
|
|
DeleteReport: func(id restic.ID, err error) {
|
|
|
|
if err != nil {
|
|
|
|
printer.VV("failed to remove index %v: %v\n", id.String(), err)
|
|
|
|
} else {
|
|
|
|
printer.VV("removed index %v\n", id.String())
|
|
|
|
}
|
2024-04-06 17:12:04 +00:00
|
|
|
},
|
|
|
|
})
|
|
|
|
}
|