2016-08-01 16:55:07 +00:00
|
|
|
package repository
|
|
|
|
|
|
|
|
import (
|
2017-01-23 16:05:30 +00:00
|
|
|
"crypto/sha256"
|
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"
|
2017-05-10 17:48:22 +00:00
|
|
|
"restic/fs"
|
2017-01-23 16:05:30 +00:00
|
|
|
"restic/hashing"
|
2016-08-01 16:55:07 +00:00
|
|
|
"restic/pack"
|
2016-08-29 17:18:57 +00:00
|
|
|
|
2016-09-01 20:17:37 +00:00
|
|
|
"restic/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.
|
2017-03-04 16:38:34 +00:00
|
|
|
func Repack(repo restic.Repository, packs restic.IDSet, keepBlobs restic.BlobSet, p *restic.Progress) (err error) {
|
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
|
2016-09-01 19:19:30 +00:00
|
|
|
h := restic.Handle{Type: restic.DataFile, Name: packID.String()}
|
2016-08-01 16:55:07 +00:00
|
|
|
|
2017-05-10 17:48:22 +00:00
|
|
|
tempfile, err := fs.TempFile("", "restic-temp-repack-")
|
2017-01-23 16:05:30 +00:00
|
|
|
if err != nil {
|
|
|
|
return errors.Wrap(err, "TempFile")
|
2016-08-01 16:55:07 +00:00
|
|
|
}
|
|
|
|
|
2017-01-23 17:11:10 +00:00
|
|
|
beRd, err := repo.Backend().Load(h, 0, 0)
|
2016-08-01 16:55:07 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2017-01-23 16:05:30 +00:00
|
|
|
hrd := hashing.NewReader(beRd, sha256.New())
|
|
|
|
packLength, err := io.Copy(tempfile, hrd)
|
|
|
|
if err != nil {
|
|
|
|
return errors.Wrap(err, "Copy")
|
|
|
|
}
|
|
|
|
|
2017-02-05 14:40:30 +00:00
|
|
|
if err = beRd.Close(); err != nil {
|
|
|
|
return errors.Wrap(err, "Close")
|
|
|
|
}
|
|
|
|
|
2017-01-23 16:05:30 +00:00
|
|
|
hash := restic.IDFromHash(hrd.Sum(nil))
|
|
|
|
debug.Log("pack %v loaded (%d bytes), hash %v", packID.Str(), packLength, hash.Str())
|
|
|
|
|
|
|
|
if !packID.Equal(hash) {
|
|
|
|
return errors.Errorf("hash does not match id: want %v, got %v", packID, hash)
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err = tempfile.Seek(0, 0)
|
|
|
|
if err != nil {
|
|
|
|
return errors.Wrap(err, "Seek")
|
|
|
|
}
|
|
|
|
|
|
|
|
blobs, err := pack.List(repo.Key(), tempfile, packLength)
|
2016-08-01 16:55:07 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2016-09-27 20:35:08 +00:00
|
|
|
debug.Log("processing pack %v, blobs: %v", packID.Str(), 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
|
|
|
|
2017-01-23 16:05:30 +00:00
|
|
|
buf = buf[:len(buf)]
|
|
|
|
if uint(len(buf)) < entry.Length {
|
|
|
|
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 {
|
|
|
|
return errors.Wrap(err, "ReadAt")
|
|
|
|
}
|
|
|
|
|
|
|
|
if n != len(buf) {
|
|
|
|
return errors.Errorf("read blob %v from %v: not enough bytes read, want %v, got %v",
|
|
|
|
h, tempfile.Name(), len(buf), n)
|
|
|
|
}
|
2016-09-03 11:34:04 +00:00
|
|
|
|
2017-01-23 16:05:30 +00:00
|
|
|
n, err = crypto.Decrypt(repo.Key(), buf, buf)
|
2016-08-01 16:55:07 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2017-01-23 16:05:30 +00:00
|
|
|
buf = buf[:n]
|
|
|
|
|
|
|
|
id := restic.Hash(buf)
|
|
|
|
if !id.Equal(entry.ID) {
|
|
|
|
return errors.Errorf("read blob %v from %v: wrong data returned, hash is %v",
|
|
|
|
h, tempfile.Name(), id)
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err = repo.SaveBlob(entry.Type, buf, entry.ID)
|
2016-08-01 16:55:07 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2016-09-27 20:35:08 +00:00
|
|
|
debug.Log(" saved blob %v", entry.ID.Str())
|
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 {
|
|
|
|
return errors.Wrap(err, "Close")
|
|
|
|
}
|
|
|
|
|
2017-05-10 17:48:22 +00:00
|
|
|
if err = fs.RemoveIfExists(tempfile.Name()); err != nil {
|
2017-01-23 16:05:30 +00:00
|
|
|
return errors.Wrap(err, "Remove")
|
|
|
|
}
|
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
|
|
|
}
|
|
|
|
|
|
|
|
if err := repo.Flush(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
for packID := range packs {
|
2017-01-25 16:48:35 +00:00
|
|
|
h := restic.Handle{Type: restic.DataFile, Name: packID.String()}
|
|
|
|
err := repo.Backend().Remove(h)
|
2016-08-01 16:55:07 +00:00
|
|
|
if err != nil {
|
2016-09-27 20:35:08 +00:00
|
|
|
debug.Log("error removing pack %v: %v", packID.Str(), err)
|
2016-08-01 16:55:07 +00:00
|
|
|
return err
|
|
|
|
}
|
2016-09-27 20:35:08 +00:00
|
|
|
debug.Log("removed pack %v", packID.Str())
|
2016-08-01 16:55:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|