mirror of
https://github.com/octoleo/restic.git
synced 2024-12-27 12:42:49 +00:00
filesrestorer: Re-use buffer
This commit is contained in:
parent
3e0acf1395
commit
07b3f65a6f
@ -253,13 +253,14 @@ func (r *fileRestorer) downloadPack(ctx context.Context, pack *packInfo) {
|
|||||||
}
|
}
|
||||||
BufRd := bufio.NewReaderSize(rd, bufferSize)
|
BufRd := bufio.NewReaderSize(rd, bufferSize)
|
||||||
currentBlobEnd := start
|
currentBlobEnd := start
|
||||||
|
var blobData, buf []byte
|
||||||
for _, blobID := range sortedBlobs {
|
for _, blobID := range sortedBlobs {
|
||||||
blob := blobs[blobID]
|
blob := blobs[blobID]
|
||||||
_, err := BufRd.Discard(int(blob.offset - currentBlobEnd))
|
_, err := BufRd.Discard(int(blob.offset - currentBlobEnd))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
blobData, err := r.loadBlob(BufRd, blobID, blob.length)
|
blobData, buf, err = r.loadBlob(BufRd, blobID, blob.length, buf)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
for file := range blob.files {
|
for file := range blob.files {
|
||||||
markFileError(file, err)
|
markFileError(file, err)
|
||||||
@ -309,31 +310,35 @@ func (r *fileRestorer) downloadPack(ctx context.Context, pack *packInfo) {
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (r *fileRestorer) loadBlob(rd io.Reader, blobID restic.ID, length int) ([]byte, error) {
|
func (r *fileRestorer) loadBlob(rd io.Reader, blobID restic.ID, length int, buf []byte) ([]byte, []byte, error) {
|
||||||
// TODO reconcile with Repository#loadBlob implementation
|
// TODO reconcile with Repository#loadBlob implementation
|
||||||
|
|
||||||
buf := make([]byte, length)
|
if cap(buf) < length {
|
||||||
|
buf = make([]byte, length)
|
||||||
|
} else {
|
||||||
|
buf = buf[:length]
|
||||||
|
}
|
||||||
|
|
||||||
n, err := rd.Read(buf)
|
n, err := rd.Read(buf)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
if n != length {
|
if n != length {
|
||||||
return nil, errors.Errorf("error loading blob %v: wrong length returned, want %d, got %d", blobID.Str(), length, n)
|
return nil, nil, errors.Errorf("error loading blob %v: wrong length returned, want %d, got %d", blobID.Str(), length, n)
|
||||||
}
|
}
|
||||||
|
|
||||||
// decrypt
|
// decrypt
|
||||||
nonce, ciphertext := buf[:r.key.NonceSize()], buf[r.key.NonceSize():]
|
nonce, ciphertext := buf[:r.key.NonceSize()], buf[r.key.NonceSize():]
|
||||||
plaintext, err := r.key.Open(ciphertext[:0], nonce, ciphertext, nil)
|
plaintext, err := r.key.Open(ciphertext[:0], nonce, ciphertext, nil)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, errors.Errorf("decrypting blob %v failed: %v", blobID, err)
|
return nil, nil, errors.Errorf("decrypting blob %v failed: %v", blobID, err)
|
||||||
}
|
}
|
||||||
|
|
||||||
// check hash
|
// check hash
|
||||||
if !restic.Hash(plaintext).Equal(blobID) {
|
if !restic.Hash(plaintext).Equal(blobID) {
|
||||||
return nil, errors.Errorf("blob %v returned invalid hash", blobID)
|
return nil, nil, errors.Errorf("blob %v returned invalid hash", blobID)
|
||||||
}
|
}
|
||||||
|
|
||||||
return plaintext, nil
|
return plaintext, buf, nil
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user