mirror of
https://github.com/octoleo/restic.git
synced 2024-06-02 17:10:49 +00:00
b9bddeff39
This makes the following changes, before: type backend interface { // Test a boolean value whether a File with the name and type exists. Test(t FileType, name string) (bool, error) // Remove removes a File with type t and name. Remove(t FileType, name string) error } After: type backend interface { // Test a boolean value whether a File with the name and type exists. Test(h Handle) (bool, error) // Remove removes a File with type t and name. Remove(h Handle) error }
67 lines
1.4 KiB
Go
67 lines
1.4 KiB
Go
package repository
|
|
|
|
import (
|
|
"fmt"
|
|
"os"
|
|
"restic"
|
|
"restic/debug"
|
|
"restic/list"
|
|
"restic/worker"
|
|
)
|
|
|
|
// RebuildIndex lists all packs in the repo, writes a new index and removes all
|
|
// old indexes. This operation should only be done with an exclusive lock in
|
|
// place.
|
|
func RebuildIndex(repo restic.Repository) error {
|
|
debug.Log("start rebuilding index")
|
|
|
|
done := make(chan struct{})
|
|
defer close(done)
|
|
|
|
ch := make(chan worker.Job)
|
|
go list.AllPacks(repo, ch, done)
|
|
|
|
idx := NewIndex()
|
|
for job := range ch {
|
|
id := job.Data.(restic.ID)
|
|
|
|
if job.Error != nil {
|
|
fmt.Fprintf(os.Stderr, "error for pack %v: %v\n", id, job.Error)
|
|
continue
|
|
}
|
|
|
|
res := job.Result.(list.Result)
|
|
|
|
for _, entry := range res.Entries() {
|
|
pb := restic.PackedBlob{
|
|
Blob: entry,
|
|
PackID: res.PackID(),
|
|
}
|
|
idx.Store(pb)
|
|
}
|
|
}
|
|
|
|
oldIndexes := restic.NewIDSet()
|
|
for id := range repo.List(restic.IndexFile, done) {
|
|
idx.AddToSupersedes(id)
|
|
oldIndexes.Insert(id)
|
|
}
|
|
|
|
id, err := SaveIndex(repo, idx)
|
|
if err != nil {
|
|
debug.Log("error saving index: %v", err)
|
|
return err
|
|
}
|
|
debug.Log("new index saved as %v", id.Str())
|
|
|
|
for indexID := range oldIndexes {
|
|
h := restic.Handle{Type: restic.IndexFile, Name: indexID.String()}
|
|
err := repo.Backend().Remove(h)
|
|
if err != nil {
|
|
fmt.Fprintf(os.Stderr, "unable to remove index %v: %v\n", indexID.Str(), err)
|
|
}
|
|
}
|
|
|
|
return nil
|
|
}
|