2015-07-25 12:20:02 +00:00
|
|
|
package checker
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
|
|
|
|
"github.com/restic/restic/backend"
|
|
|
|
"github.com/restic/restic/debug"
|
|
|
|
"github.com/restic/restic/repository"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Repacker extracts still used blobs from packs with unused blobs and creates
|
|
|
|
// new packs.
|
|
|
|
type Repacker struct {
|
2015-11-01 21:45:52 +00:00
|
|
|
unusedBlobs backend.IDSet
|
2015-07-25 12:20:02 +00:00
|
|
|
src, dst *repository.Repository
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewRepacker returns a new repacker that (when Repack() in run) cleans up the
|
|
|
|
// repository and creates new packs and indexs so that all blobs in unusedBlobs
|
|
|
|
// aren't used any more.
|
2015-11-01 21:45:52 +00:00
|
|
|
func NewRepacker(src, dst *repository.Repository, unusedBlobs backend.IDSet) *Repacker {
|
2015-07-25 12:20:02 +00:00
|
|
|
return &Repacker{
|
|
|
|
src: src,
|
|
|
|
dst: dst,
|
|
|
|
unusedBlobs: unusedBlobs,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Repack runs the process of finding still used blobs in packs with unused
|
|
|
|
// blobs, extracts them and creates new packs with just the still-in-use blobs.
|
|
|
|
func (r *Repacker) Repack() error {
|
|
|
|
debug.Log("Repacker.Repack", "searching packs for %v", r.unusedBlobs)
|
2015-11-01 21:45:52 +00:00
|
|
|
|
|
|
|
packs, err := FindPacksForBlobs(r.src, r.unusedBlobs)
|
2015-07-25 12:20:02 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
debug.Log("Repacker.Repack", "found packs: %v", packs)
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2015-11-01 21:45:52 +00:00
|
|
|
// FindPacksForBlobs returns the set of packs that contain the blobs.
|
|
|
|
func FindPacksForBlobs(repo *repository.Repository, blobs backend.IDSet) (backend.IDSet, error) {
|
2015-07-25 12:20:02 +00:00
|
|
|
packs := backend.NewIDSet()
|
|
|
|
idx := repo.Index()
|
2015-11-01 21:45:52 +00:00
|
|
|
for id := range blobs {
|
2015-07-25 12:20:02 +00:00
|
|
|
blob, err := idx.Lookup(id)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
packs.Insert(blob.PackID)
|
|
|
|
}
|
|
|
|
|
|
|
|
return packs, nil
|
|
|
|
}
|
|
|
|
|
2015-11-01 21:45:52 +00:00
|
|
|
// FindBlobsForPacks returns the set of blobs contained in a pack of packs.
|
|
|
|
func FindBlobsForPacks(repo *repository.Repository, packs backend.IDSet) (backend.IDSet, error) {
|
|
|
|
blobs := backend.NewIDSet()
|
|
|
|
|
|
|
|
for packID := range packs {
|
|
|
|
for _, packedBlob := range repo.Index().ListPack(packID) {
|
|
|
|
blobs.Insert(packedBlob.ID)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return blobs, nil
|
|
|
|
}
|
|
|
|
|
2015-07-25 12:20:02 +00:00
|
|
|
// repackBlob loads a single blob from src and saves it in dst.
|
|
|
|
func repackBlob(src, dst *repository.Repository, id backend.ID) error {
|
|
|
|
blob, err := src.Index().Lookup(id)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
debug.Log("RepackBlobs", "repacking blob %v, len %v", id.Str(), blob.PlaintextLength())
|
|
|
|
|
|
|
|
buf := make([]byte, 0, blob.PlaintextLength())
|
|
|
|
buf, err = src.LoadBlob(blob.Type, id, buf)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if uint(len(buf)) != blob.PlaintextLength() {
|
|
|
|
debug.Log("RepackBlobs", "repack blob %v: len(buf) isn't equal to length: %v = %v", id.Str(), len(buf), blob.PlaintextLength())
|
|
|
|
return errors.New("LoadBlob returned wrong data, len() doesn't match")
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err = dst.SaveAndEncrypt(blob.Type, buf, &id)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// RepackBlobs reads all blobs in blobIDs from src and saves them into new pack
|
|
|
|
// files in dst. Source and destination repo may be the same.
|
|
|
|
func RepackBlobs(src, dst *repository.Repository, blobIDs backend.IDs) (err error) {
|
|
|
|
for _, id := range blobIDs {
|
|
|
|
err = repackBlob(src, dst, id)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
err = dst.Flush()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
err = dst.SaveIndex()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|