/
fetch.go
95 lines (80 loc) · 2.36 KB
/
fetch.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
package rindex
import (
"context"
"fmt"
"io"
"github.com/pkg/errors"
"github.com/rubiojr/rapi"
"github.com/rubiojr/rapi/repository"
"github.com/rubiojr/rapi/restic"
"github.com/vmihailenco/msgpack/v5"
)
func (i *Indexer) Fetch(ctx context.Context, fileID string, writer io.Writer) error {
ropts := rapi.DefaultOptions
ropts.Password = i.RepositoryPassword
ropts.Repo = i.RepositoryLocation
repo, err := rapi.OpenRepository(ropts)
if err != nil {
return err
}
var decodeError error
var pblobs []restic.PackedBlob
_, err = i.Search(fmt.Sprintf("_id:%s", fileID), func(field string, value []byte) bool {
if field == "blobs" {
decodeError = msgpack.Unmarshal(value, &pblobs)
}
return true
}, nil)
if err != nil {
return err
}
if decodeError != nil {
return fmt.Errorf("error unmarshalling blobs: %v", decodeError)
}
if len(pblobs) == 0 {
return errors.Errorf("no blobs found for %s", fileID)
}
var buf []byte
for _, blob := range pblobs {
plaintext, err := loadBlob(ctx, repo, buf, blob)
if err != nil {
return err
}
writer.Write(plaintext)
}
return nil
}
// Retrieve and decrypt blob from repository.
// Similar to Restic's repository.LoadBlob but without using the in memory index, saving us tons of
// memory in large repos.
func loadBlob(ctx context.Context, r *repository.Repository, buf []byte, blob restic.PackedBlob) ([]byte, error) {
// load blob from pack
h := restic.Handle{Type: restic.PackFile, Name: blob.PackID.String()}
switch {
case cap(buf) < int(blob.Length):
buf = make([]byte, blob.Length)
case len(buf) != int(blob.Length):
buf = buf[:blob.Length]
}
n, err := restic.ReadAt(ctx, r.Backend(), h, int64(blob.Offset), buf)
if err != nil {
return nil, err
}
if uint(n) != blob.Length {
return nil, errors.Errorf("error loading blob %v: wrong length returned, want %d, got %d",
blob.ID.Str(), blob.Length, uint(n))
}
// decrypt
nonce, ciphertext := buf[:r.Key().NonceSize()], buf[r.Key().NonceSize():]
plaintext, err := r.Key().Open(ciphertext[:0], nonce, ciphertext, nil)
if err != nil {
return nil, errors.Errorf("decrypting blob %v failed: %v", blob.ID, err)
}
// check hash
if !restic.Hash(plaintext).Equal(blob.ID) {
return nil, errors.Errorf("blob %v returned invalid hash", blob.ID)
}
// move decrypted data to the start of the buffer
copy(buf, plaintext)
return buf[:len(plaintext)], nil
}