2018-04-08 12:02:30 +00:00
|
|
|
package restorer
|
|
|
|
|
|
|
|
import (
|
2019-11-27 12:22:38 +00:00
|
|
|
"bytes"
|
2018-04-08 12:02:30 +00:00
|
|
|
"context"
|
|
|
|
"io"
|
2019-11-27 12:22:38 +00:00
|
|
|
"math"
|
2018-09-15 00:18:37 +00:00
|
|
|
"path/filepath"
|
2019-11-27 12:22:38 +00:00
|
|
|
"sync"
|
2018-04-08 12:02:30 +00:00
|
|
|
|
|
|
|
"github.com/restic/restic/internal/crypto"
|
|
|
|
"github.com/restic/restic/internal/debug"
|
|
|
|
"github.com/restic/restic/internal/errors"
|
|
|
|
"github.com/restic/restic/internal/restic"
|
|
|
|
)
|
|
|
|
|
|
|
|
// TODO if a blob is corrupt, there may be good blob copies in other packs
|
|
|
|
// TODO evaluate if it makes sense to split download and processing workers
|
|
|
|
// pro: can (slowly) read network and decrypt/write files concurrently
|
|
|
|
// con: each worker needs to keep one pack in memory
|
|
|
|
|
|
|
|
const (
|
|
|
|
workerCount = 8
|
|
|
|
|
2019-11-27 12:22:38 +00:00
|
|
|
// fileInfo flags
|
|
|
|
fileProgress = 1
|
|
|
|
fileError = 2
|
2018-04-08 12:02:30 +00:00
|
|
|
|
2019-11-27 12:22:38 +00:00
|
|
|
largeFileBlobCount = 25
|
2018-04-08 12:02:30 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// information about regular file being restored
|
|
|
|
type fileInfo struct {
|
2019-11-27 12:22:38 +00:00
|
|
|
lock sync.Mutex
|
|
|
|
flags int
|
2018-09-15 00:18:37 +00:00
|
|
|
location string // file on local filesystem relative to restorer basedir
|
2019-11-27 12:22:38 +00:00
|
|
|
blobs interface{} // blobs of the file
|
|
|
|
}
|
|
|
|
|
|
|
|
type fileBlobInfo struct {
|
|
|
|
id restic.ID // the blob id
|
|
|
|
offset int64 // blob offset in the file
|
2018-04-08 12:02:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// information about a data pack required to restore one or more files
|
|
|
|
type packInfo struct {
|
2019-11-27 12:22:38 +00:00
|
|
|
id restic.ID // the pack id
|
|
|
|
files map[*fileInfo]struct{} // set of files that use blobs from this pack
|
2018-04-08 12:02:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// fileRestorer restores set of files
|
|
|
|
type fileRestorer struct {
|
|
|
|
key *crypto.Key
|
2020-06-14 11:26:10 +00:00
|
|
|
idx func(restic.ID, restic.BlobType) []restic.PackedBlob
|
2018-04-08 12:02:30 +00:00
|
|
|
packLoader func(ctx context.Context, h restic.Handle, length int, offset int64, fn func(rd io.Reader) error) error
|
|
|
|
|
2019-11-27 12:22:38 +00:00
|
|
|
filesWriter *filesWriter
|
2018-04-08 12:02:30 +00:00
|
|
|
|
2018-09-15 00:18:37 +00:00
|
|
|
dst string
|
2018-04-08 12:02:30 +00:00
|
|
|
files []*fileInfo
|
|
|
|
}
|
|
|
|
|
2019-11-27 12:22:38 +00:00
|
|
|
func newFileRestorer(dst string,
|
|
|
|
packLoader func(ctx context.Context, h restic.Handle, length int, offset int64, fn func(rd io.Reader) error) error,
|
|
|
|
key *crypto.Key,
|
2020-06-14 11:26:10 +00:00
|
|
|
idx func(restic.ID, restic.BlobType) []restic.PackedBlob) *fileRestorer {
|
2019-11-27 12:22:38 +00:00
|
|
|
|
2018-04-08 12:02:30 +00:00
|
|
|
return &fileRestorer{
|
|
|
|
key: key,
|
|
|
|
idx: idx,
|
2019-11-27 12:22:38 +00:00
|
|
|
packLoader: packLoader,
|
|
|
|
filesWriter: newFilesWriter(workerCount),
|
2018-09-15 00:18:37 +00:00
|
|
|
dst: dst,
|
2018-04-08 12:02:30 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-09-15 00:18:37 +00:00
|
|
|
func (r *fileRestorer) addFile(location string, content restic.IDs) {
|
|
|
|
r.files = append(r.files, &fileInfo{location: location, blobs: content})
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *fileRestorer) targetPath(location string) string {
|
|
|
|
return filepath.Join(r.dst, location)
|
2018-04-08 12:02:30 +00:00
|
|
|
}
|
|
|
|
|
2019-11-27 12:22:38 +00:00
|
|
|
func (r *fileRestorer) forEachBlob(blobIDs []restic.ID, fn func(packID restic.ID, packBlob restic.Blob)) error {
|
|
|
|
if len(blobIDs) == 0 {
|
|
|
|
return nil
|
|
|
|
}
|
2018-04-08 12:02:30 +00:00
|
|
|
|
2019-11-27 12:22:38 +00:00
|
|
|
for _, blobID := range blobIDs {
|
2020-06-14 11:26:10 +00:00
|
|
|
packs := r.idx(blobID, restic.DataBlob)
|
|
|
|
if len(packs) == 0 {
|
2019-11-27 12:22:38 +00:00
|
|
|
return errors.Errorf("Unknown blob %s", blobID.String())
|
2018-04-08 12:02:30 +00:00
|
|
|
}
|
2019-11-27 12:22:38 +00:00
|
|
|
fn(packs[0].PackID, packs[0].Blob)
|
2018-04-08 12:02:30 +00:00
|
|
|
}
|
|
|
|
|
2019-11-27 12:22:38 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *fileRestorer) restoreFiles(ctx context.Context) error {
|
|
|
|
|
|
|
|
packs := make(map[restic.ID]*packInfo) // all packs
|
2018-04-08 12:02:30 +00:00
|
|
|
|
2019-11-27 12:22:38 +00:00
|
|
|
// create packInfo from fileInfo
|
|
|
|
for _, file := range r.files {
|
|
|
|
fileBlobs := file.blobs.(restic.IDs)
|
|
|
|
largeFile := len(fileBlobs) > largeFileBlobCount
|
|
|
|
var packsMap map[restic.ID][]fileBlobInfo
|
|
|
|
if largeFile {
|
|
|
|
packsMap = make(map[restic.ID][]fileBlobInfo)
|
|
|
|
}
|
|
|
|
fileOffset := int64(0)
|
|
|
|
err := r.forEachBlob(fileBlobs, func(packID restic.ID, blob restic.Blob) {
|
|
|
|
if largeFile {
|
|
|
|
packsMap[packID] = append(packsMap[packID], fileBlobInfo{id: blob.ID, offset: fileOffset})
|
|
|
|
fileOffset += int64(blob.Length) - crypto.Extension
|
|
|
|
}
|
|
|
|
pack, ok := packs[packID]
|
|
|
|
if !ok {
|
|
|
|
pack = &packInfo{
|
|
|
|
id: packID,
|
|
|
|
files: make(map[*fileInfo]struct{}),
|
|
|
|
}
|
|
|
|
packs[packID] = pack
|
|
|
|
}
|
|
|
|
pack.files[file] = struct{}{}
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
// repository index is messed up, can't do anything
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if largeFile {
|
|
|
|
file.blobs = packsMap
|
|
|
|
}
|
|
|
|
}
|
2018-04-08 12:02:30 +00:00
|
|
|
|
2019-11-27 12:22:38 +00:00
|
|
|
var wg sync.WaitGroup
|
|
|
|
downloadCh := make(chan *packInfo)
|
2018-04-08 12:02:30 +00:00
|
|
|
worker := func() {
|
2019-11-27 12:22:38 +00:00
|
|
|
defer wg.Done()
|
2018-04-08 12:02:30 +00:00
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case <-ctx.Done():
|
2019-11-27 12:22:38 +00:00
|
|
|
return // context cancelled
|
|
|
|
case pack, ok := <-downloadCh:
|
2018-04-08 12:02:30 +00:00
|
|
|
if !ok {
|
|
|
|
return // channel closed
|
|
|
|
}
|
2019-11-27 12:22:38 +00:00
|
|
|
r.downloadPack(ctx, pack)
|
2018-04-08 12:02:30 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for i := 0; i < workerCount; i++ {
|
2019-11-27 12:22:38 +00:00
|
|
|
wg.Add(1)
|
2020-02-26 23:07:49 +00:00
|
|
|
go worker()
|
2018-04-08 12:02:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// the main restore loop
|
2019-11-27 12:22:38 +00:00
|
|
|
for _, pack := range packs {
|
|
|
|
select {
|
|
|
|
case <-ctx.Done():
|
|
|
|
return ctx.Err()
|
|
|
|
case downloadCh <- pack:
|
|
|
|
debug.Log("Scheduled download pack %s", pack.id.Str())
|
2018-04-08 12:02:30 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-27 12:22:38 +00:00
|
|
|
close(downloadCh)
|
|
|
|
wg.Wait()
|
|
|
|
|
2018-04-08 12:02:30 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-11-27 12:22:38 +00:00
|
|
|
func (r *fileRestorer) downloadPack(ctx context.Context, pack *packInfo) {
|
2018-04-08 12:02:30 +00:00
|
|
|
|
2019-11-27 12:22:38 +00:00
|
|
|
// calculate pack byte range and blob->[]files->[]offsets mappings
|
|
|
|
start, end := int64(math.MaxInt64), int64(0)
|
|
|
|
blobs := make(map[restic.ID]struct {
|
|
|
|
offset int64 // offset of the blob in the pack
|
|
|
|
length int // length of the blob
|
|
|
|
files map[*fileInfo][]int64 // file -> offsets (plural!) of the blob in the file
|
|
|
|
})
|
2018-04-08 12:02:30 +00:00
|
|
|
for file := range pack.files {
|
2019-11-27 12:22:38 +00:00
|
|
|
addBlob := func(blob restic.Blob, fileOffset int64) {
|
|
|
|
if start > int64(blob.Offset) {
|
|
|
|
start = int64(blob.Offset)
|
|
|
|
}
|
|
|
|
if end < int64(blob.Offset+blob.Length) {
|
|
|
|
end = int64(blob.Offset + blob.Length)
|
|
|
|
}
|
|
|
|
blobInfo, ok := blobs[blob.ID]
|
|
|
|
if !ok {
|
|
|
|
blobInfo.offset = int64(blob.Offset)
|
|
|
|
blobInfo.length = int(blob.Length)
|
|
|
|
blobInfo.files = make(map[*fileInfo][]int64)
|
|
|
|
blobs[blob.ID] = blobInfo
|
|
|
|
}
|
|
|
|
blobInfo.files[file] = append(blobInfo.files[file], fileOffset)
|
|
|
|
}
|
|
|
|
if fileBlobs, ok := file.blobs.(restic.IDs); ok {
|
|
|
|
fileOffset := int64(0)
|
|
|
|
r.forEachBlob(fileBlobs, func(packID restic.ID, blob restic.Blob) {
|
|
|
|
if packID.Equal(pack.id) {
|
|
|
|
addBlob(blob, fileOffset)
|
|
|
|
}
|
|
|
|
fileOffset += int64(blob.Length) - crypto.Extension
|
|
|
|
})
|
|
|
|
} else if packsMap, ok := file.blobs.(map[restic.ID][]fileBlobInfo); ok {
|
|
|
|
for _, blob := range packsMap[pack.id] {
|
2020-06-14 11:26:10 +00:00
|
|
|
idxPacks := r.idx(blob.id, restic.DataBlob)
|
|
|
|
for _, idxPack := range idxPacks {
|
|
|
|
if idxPack.PackID.Equal(pack.id) {
|
|
|
|
addBlob(idxPack.Blob, blob.offset)
|
|
|
|
break
|
2018-04-08 12:02:30 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-11-27 12:22:38 +00:00
|
|
|
}
|
2018-04-08 12:02:30 +00:00
|
|
|
}
|
|
|
|
|
2019-11-27 12:22:38 +00:00
|
|
|
packData := make([]byte, int(end-start))
|
2018-04-08 12:02:30 +00:00
|
|
|
|
2019-11-27 12:22:38 +00:00
|
|
|
h := restic.Handle{Type: restic.DataFile, Name: pack.id.String()}
|
|
|
|
err := r.packLoader(ctx, h, int(end-start), start, func(rd io.Reader) error {
|
|
|
|
l, err := io.ReadFull(rd, packData)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if l != len(packData) {
|
|
|
|
return errors.Errorf("unexpected pack size: expected %d but got %d", len(packData), l)
|
|
|
|
}
|
|
|
|
return nil
|
2018-04-08 12:02:30 +00:00
|
|
|
})
|
2019-11-27 12:22:38 +00:00
|
|
|
|
|
|
|
markFileError := func(file *fileInfo, err error) {
|
|
|
|
file.lock.Lock()
|
|
|
|
defer file.lock.Unlock()
|
|
|
|
if file.flags&fileError == 0 {
|
|
|
|
file.flags |= fileError
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-04-08 12:02:30 +00:00
|
|
|
if err != nil {
|
2019-11-27 12:22:38 +00:00
|
|
|
for file := range pack.files {
|
|
|
|
markFileError(file, err)
|
|
|
|
}
|
|
|
|
return
|
2018-04-08 12:02:30 +00:00
|
|
|
}
|
|
|
|
|
2019-11-27 12:22:38 +00:00
|
|
|
rd := bytes.NewReader(packData)
|
2018-04-08 12:02:30 +00:00
|
|
|
|
2019-11-27 12:22:38 +00:00
|
|
|
for blobID, blob := range blobs {
|
|
|
|
blobData, err := r.loadBlob(rd, blobID, blob.offset-start, blob.length)
|
|
|
|
if err != nil {
|
|
|
|
for file := range blob.files {
|
|
|
|
markFileError(file, err)
|
|
|
|
}
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
for file, offsets := range blob.files {
|
|
|
|
for _, offset := range offsets {
|
|
|
|
writeToFile := func() error {
|
|
|
|
// this looks overly complicated and needs explanation
|
|
|
|
// two competing requirements:
|
|
|
|
// - must create the file once and only once
|
|
|
|
// - should allow concurrent writes to the file
|
|
|
|
// so write the first blob while holding file lock
|
|
|
|
// write other blobs after releasing the lock
|
|
|
|
file.lock.Lock()
|
|
|
|
create := file.flags&fileProgress == 0
|
|
|
|
if create {
|
|
|
|
defer file.lock.Unlock()
|
|
|
|
file.flags |= fileProgress
|
|
|
|
} else {
|
|
|
|
file.lock.Unlock()
|
|
|
|
}
|
|
|
|
return r.filesWriter.writeToFile(r.targetPath(file.location), blobData, offset, create)
|
2018-04-08 12:02:30 +00:00
|
|
|
}
|
2019-11-27 12:22:38 +00:00
|
|
|
err := writeToFile()
|
2018-04-08 12:02:30 +00:00
|
|
|
if err != nil {
|
2019-11-27 12:22:38 +00:00
|
|
|
markFileError(file, err)
|
|
|
|
break
|
2018-04-08 12:02:30 +00:00
|
|
|
}
|
|
|
|
}
|
2019-11-27 12:22:38 +00:00
|
|
|
}
|
2018-04-08 12:02:30 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-27 12:22:38 +00:00
|
|
|
func (r *fileRestorer) loadBlob(rd io.ReaderAt, blobID restic.ID, offset int64, length int) ([]byte, error) {
|
2018-04-08 12:02:30 +00:00
|
|
|
// TODO reconcile with Repository#loadBlob implementation
|
|
|
|
|
2019-11-27 12:22:38 +00:00
|
|
|
buf := make([]byte, length)
|
2018-04-08 12:02:30 +00:00
|
|
|
|
2019-11-27 12:22:38 +00:00
|
|
|
n, err := rd.ReadAt(buf, offset)
|
2018-04-08 12:02:30 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2019-11-27 12:22:38 +00:00
|
|
|
if n != length {
|
|
|
|
return nil, errors.Errorf("error loading blob %v: wrong length returned, want %d, got %d", blobID.Str(), length, n)
|
2018-04-08 12:02:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// decrypt
|
|
|
|
nonce, ciphertext := buf[:r.key.NonceSize()], buf[r.key.NonceSize():]
|
|
|
|
plaintext, err := r.key.Open(ciphertext[:0], nonce, ciphertext, nil)
|
|
|
|
if err != nil {
|
2019-11-27 12:22:38 +00:00
|
|
|
return nil, errors.Errorf("decrypting blob %v failed: %v", blobID, err)
|
2018-04-08 12:02:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// check hash
|
2019-11-27 12:22:38 +00:00
|
|
|
if !restic.Hash(plaintext).Equal(blobID) {
|
|
|
|
return nil, errors.Errorf("blob %v returned invalid hash", blobID)
|
2018-04-08 12:02:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return plaintext, nil
|
|
|
|
}
|