2016-08-01 16:55:07 +00:00
|
|
|
package repository
|
|
|
|
|
|
|
|
import (
|
2017-06-04 09:16:55 +00:00
|
|
|
"context"
|
2016-08-29 17:18:57 +00:00
|
|
|
|
2017-07-23 12:21:03 +00:00
|
|
|
"github.com/restic/restic/internal/debug"
|
2018-10-28 20:12:15 +00:00
|
|
|
"github.com/restic/restic/internal/errors"
|
2017-07-23 12:21:03 +00:00
|
|
|
"github.com/restic/restic/internal/fs"
|
|
|
|
"github.com/restic/restic/internal/pack"
|
2017-07-24 15:42:25 +00:00
|
|
|
"github.com/restic/restic/internal/restic"
|
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
|
2017-06-15 12:40:34 +00:00
|
|
|
// into a new pack. Returned is the list of obsolete packs which can then
|
|
|
|
// be removed.
|
|
|
|
func Repack(ctx context.Context, repo restic.Repository, packs restic.IDSet, keepBlobs restic.BlobSet, p *restic.Progress) (obsoletePacks restic.IDSet, err error) {
|
2020-08-03 17:32:46 +00:00
|
|
|
if p != nil {
|
|
|
|
p.Start()
|
|
|
|
defer p.Done()
|
|
|
|
}
|
|
|
|
|
2016-09-27 20:35:08 +00:00
|
|
|
debug.Log("repacking %d packs while keeping %d blobs", len(packs), len(keepBlobs))
|
2016-08-01 16:55:07 +00:00
|
|
|
|
|
|
|
for packID := range packs {
|
2017-01-23 16:05:30 +00:00
|
|
|
// load the complete pack into a temp file
|
2020-08-16 09:16:38 +00:00
|
|
|
h := restic.Handle{Type: restic.PackFile, Name: packID.String()}
|
2016-08-01 16:55:07 +00:00
|
|
|
|
2018-10-28 20:12:15 +00:00
|
|
|
tempfile, hash, packLength, err := DownloadAndHash(ctx, repo.Backend(), h)
|
2016-08-01 16:55:07 +00:00
|
|
|
if err != nil {
|
2018-01-17 04:59:16 +00:00
|
|
|
return nil, errors.Wrap(err, "Repack")
|
2016-08-01 16:55:07 +00:00
|
|
|
}
|
|
|
|
|
2018-01-25 19:49:41 +00:00
|
|
|
debug.Log("pack %v loaded (%d bytes), hash %v", packID, packLength, hash)
|
2017-01-23 16:05:30 +00:00
|
|
|
|
|
|
|
if !packID.Equal(hash) {
|
2017-06-15 12:40:34 +00:00
|
|
|
return nil, errors.Errorf("hash does not match id: want %v, got %v", packID, hash)
|
2017-01-23 16:05:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
_, err = tempfile.Seek(0, 0)
|
|
|
|
if err != nil {
|
2017-06-15 12:40:34 +00:00
|
|
|
return nil, errors.Wrap(err, "Seek")
|
2017-01-23 16:05:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
blobs, err := pack.List(repo.Key(), tempfile, packLength)
|
2016-08-01 16:55:07 +00:00
|
|
|
if err != nil {
|
2017-06-15 12:40:34 +00:00
|
|
|
return nil, err
|
2016-08-01 16:55:07 +00:00
|
|
|
}
|
|
|
|
|
2018-01-25 19:49:41 +00:00
|
|
|
debug.Log("processing pack %v, blobs: %v", packID, len(blobs))
|
2017-01-23 16:05:30 +00:00
|
|
|
var buf []byte
|
2016-08-25 19:08:16 +00:00
|
|
|
for _, entry := range blobs {
|
2016-08-31 20:39:36 +00:00
|
|
|
h := restic.BlobHandle{ID: entry.ID, Type: entry.Type}
|
2016-08-03 20:38:05 +00:00
|
|
|
if !keepBlobs.Has(h) {
|
2016-08-01 16:55:07 +00:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2016-09-27 20:35:08 +00:00
|
|
|
debug.Log(" process blob %v", h)
|
2016-08-01 16:55:07 +00:00
|
|
|
|
2020-03-31 12:58:48 +00:00
|
|
|
if uint(cap(buf)) < entry.Length {
|
2017-01-23 16:05:30 +00:00
|
|
|
buf = make([]byte, entry.Length)
|
2016-08-01 16:55:07 +00:00
|
|
|
}
|
2017-01-23 16:05:30 +00:00
|
|
|
buf = buf[:entry.Length]
|
2016-08-01 16:55:07 +00:00
|
|
|
|
2017-01-23 16:05:30 +00:00
|
|
|
n, err := tempfile.ReadAt(buf, int64(entry.Offset))
|
|
|
|
if err != nil {
|
2017-06-15 12:40:34 +00:00
|
|
|
return nil, errors.Wrap(err, "ReadAt")
|
2017-01-23 16:05:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if n != len(buf) {
|
2017-06-15 12:40:34 +00:00
|
|
|
return nil, errors.Errorf("read blob %v from %v: not enough bytes read, want %v, got %v",
|
2017-01-23 16:05:30 +00:00
|
|
|
h, tempfile.Name(), len(buf), n)
|
|
|
|
}
|
2016-09-03 11:34:04 +00:00
|
|
|
|
2017-10-29 10:33:57 +00:00
|
|
|
nonce, ciphertext := buf[:repo.Key().NonceSize()], buf[repo.Key().NonceSize():]
|
|
|
|
plaintext, err := repo.Key().Open(ciphertext[:0], nonce, ciphertext, nil)
|
2016-08-01 16:55:07 +00:00
|
|
|
if err != nil {
|
2017-06-15 12:40:34 +00:00
|
|
|
return nil, err
|
2016-08-01 16:55:07 +00:00
|
|
|
}
|
|
|
|
|
2017-10-29 10:33:57 +00:00
|
|
|
id := restic.Hash(plaintext)
|
2017-01-23 16:05:30 +00:00
|
|
|
if !id.Equal(entry.ID) {
|
2017-10-02 14:27:08 +00:00
|
|
|
debug.Log("read blob %v/%v from %v: wrong data returned, hash is %v",
|
|
|
|
h.Type, h.ID, tempfile.Name(), id)
|
2020-03-31 12:50:30 +00:00
|
|
|
return nil, errors.Errorf("read blob %v from %v: wrong data returned, hash is %v",
|
2017-01-23 16:05:30 +00:00
|
|
|
h, tempfile.Name(), id)
|
|
|
|
}
|
|
|
|
|
2020-06-06 20:20:44 +00:00
|
|
|
// We do want to save already saved blobs!
|
|
|
|
_, _, err = repo.SaveBlob(ctx, entry.Type, plaintext, entry.ID, true)
|
2016-08-01 16:55:07 +00:00
|
|
|
if err != nil {
|
2017-06-15 12:40:34 +00:00
|
|
|
return nil, err
|
2016-08-01 16:55:07 +00:00
|
|
|
}
|
|
|
|
|
2018-01-25 19:49:41 +00:00
|
|
|
debug.Log(" saved blob %v", entry.ID)
|
2016-08-01 16:55:07 +00:00
|
|
|
|
2016-08-03 20:38:05 +00:00
|
|
|
keepBlobs.Delete(h)
|
2016-08-01 16:55:07 +00:00
|
|
|
}
|
2017-01-23 16:05:30 +00:00
|
|
|
|
|
|
|
if err = tempfile.Close(); err != nil {
|
2017-06-15 12:40:34 +00:00
|
|
|
return nil, errors.Wrap(err, "Close")
|
2017-01-23 16:05:30 +00:00
|
|
|
}
|
|
|
|
|
2017-05-10 17:48:22 +00:00
|
|
|
if err = fs.RemoveIfExists(tempfile.Name()); err != nil {
|
2017-06-15 12:40:34 +00:00
|
|
|
return nil, errors.Wrap(err, "Remove")
|
2017-01-23 16:05:30 +00:00
|
|
|
}
|
2017-03-04 16:38:34 +00:00
|
|
|
if p != nil {
|
|
|
|
p.Report(restic.Stat{Blobs: 1})
|
|
|
|
}
|
2016-08-01 16:55:07 +00:00
|
|
|
}
|
|
|
|
|
2017-11-22 11:27:29 +00:00
|
|
|
if err := repo.Flush(ctx); err != nil {
|
2017-06-15 12:40:34 +00:00
|
|
|
return nil, err
|
2016-08-01 16:55:07 +00:00
|
|
|
}
|
|
|
|
|
2017-06-15 12:40:34 +00:00
|
|
|
return packs, nil
|
2016-08-01 16:55:07 +00:00
|
|
|
}
|