2016-08-01 16:55:07 +00:00
|
|
|
package repository
|
|
|
|
|
|
|
|
import (
|
2016-08-25 19:51:07 +00:00
|
|
|
"bytes"
|
2016-08-01 16:55:07 +00:00
|
|
|
"io"
|
2016-08-31 18:29:54 +00:00
|
|
|
"restic"
|
2016-08-01 16:55:07 +00:00
|
|
|
"restic/crypto"
|
|
|
|
"restic/debug"
|
|
|
|
"restic/pack"
|
2016-08-29 17:18:57 +00:00
|
|
|
|
|
|
|
"github.com/pkg/errors"
|
2016-08-01 16:55:07 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// Repack takes a list of packs together with a list of blobs contained in
|
|
|
|
// these packs. Each pack is loaded and the blobs listed in keepBlobs is saved
|
|
|
|
// into a new pack. Afterwards, the packs are removed. This operation requires
|
|
|
|
// an exclusive lock on the repo.
|
2016-08-31 18:29:54 +00:00
|
|
|
func Repack(repo *Repository, packs restic.IDSet, keepBlobs pack.BlobSet) (err error) {
|
2016-08-01 16:55:07 +00:00
|
|
|
debug.Log("Repack", "repacking %d packs while keeping %d blobs", len(packs), len(keepBlobs))
|
|
|
|
|
|
|
|
buf := make([]byte, 0, maxPackSize)
|
|
|
|
for packID := range packs {
|
|
|
|
// load the complete pack
|
2016-08-31 18:29:54 +00:00
|
|
|
h := restic.Handle{Type: restic.DataFile, Name: packID.String()}
|
2016-08-01 16:55:07 +00:00
|
|
|
|
|
|
|
l, err := repo.Backend().Load(h, buf[:cap(buf)], 0)
|
2016-08-29 17:18:57 +00:00
|
|
|
if errors.Cause(err) == io.ErrUnexpectedEOF {
|
2016-08-01 16:55:07 +00:00
|
|
|
err = nil
|
|
|
|
buf = buf[:l]
|
|
|
|
}
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
debug.Log("Repack", "pack %v loaded (%d bytes)", packID.Str(), len(buf))
|
|
|
|
|
2016-08-25 19:51:07 +00:00
|
|
|
blobs, err := pack.List(repo.Key(), bytes.NewReader(buf), int64(len(buf)))
|
2016-08-01 16:55:07 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2016-08-25 19:08:16 +00:00
|
|
|
debug.Log("Repack", "processing pack %v, blobs: %v", packID.Str(), len(blobs))
|
2016-08-01 16:55:07 +00:00
|
|
|
var plaintext []byte
|
2016-08-25 19:08:16 +00:00
|
|
|
for _, entry := range blobs {
|
2016-08-03 20:38:05 +00:00
|
|
|
h := pack.Handle{ID: entry.ID, Type: entry.Type}
|
|
|
|
if !keepBlobs.Has(h) {
|
2016-08-01 16:55:07 +00:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
ciphertext := buf[entry.Offset : entry.Offset+entry.Length]
|
|
|
|
|
|
|
|
if cap(plaintext) < len(ciphertext) {
|
|
|
|
plaintext = make([]byte, len(ciphertext))
|
|
|
|
}
|
|
|
|
|
|
|
|
plaintext, err = crypto.Decrypt(repo.Key(), plaintext, ciphertext)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err = repo.SaveAndEncrypt(entry.Type, plaintext, &entry.ID)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
debug.Log("Repack", " saved blob %v", entry.ID.Str())
|
|
|
|
|
2016-08-03 20:38:05 +00:00
|
|
|
keepBlobs.Delete(h)
|
2016-08-01 16:55:07 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := repo.Flush(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
for packID := range packs {
|
2016-08-31 18:29:54 +00:00
|
|
|
err := repo.Backend().Remove(restic.DataFile, packID.String())
|
2016-08-01 16:55:07 +00:00
|
|
|
if err != nil {
|
|
|
|
debug.Log("Repack", "error removing pack %v: %v", packID.Str(), err)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
debug.Log("Repack", "removed pack %v", packID.Str())
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|