2016-01-23 22:41:55 +00:00
|
|
|
package backend
|
|
|
|
|
2016-08-29 17:18:57 +00:00
|
|
|
import (
|
2019-03-24 20:59:14 +00:00
|
|
|
"bytes"
|
2017-06-03 15:39:57 +00:00
|
|
|
"context"
|
2021-11-06 00:14:24 +00:00
|
|
|
"fmt"
|
2016-08-29 17:18:57 +00:00
|
|
|
"io"
|
2017-07-23 12:21:03 +00:00
|
|
|
|
2021-09-19 17:58:47 +00:00
|
|
|
"github.com/restic/restic/internal/debug"
|
|
|
|
"github.com/restic/restic/internal/errors"
|
2017-07-24 15:42:25 +00:00
|
|
|
"github.com/restic/restic/internal/restic"
|
2016-08-29 17:18:57 +00:00
|
|
|
)
|
2016-02-04 18:37:33 +00:00
|
|
|
|
2019-03-24 20:59:14 +00:00
|
|
|
// LoadAll reads all data stored in the backend for the handle into the given
|
|
|
|
// buffer, which is truncated. If the buffer is not large enough or nil, a new
|
|
|
|
// one is allocated.
|
|
|
|
func LoadAll(ctx context.Context, buf []byte, be restic.Backend, h restic.Handle) ([]byte, error) {
|
2021-09-19 17:58:47 +00:00
|
|
|
retriedInvalidData := false
|
2019-03-24 20:59:14 +00:00
|
|
|
err := be.Load(ctx, h, 0, 0, func(rd io.Reader) error {
|
|
|
|
// make sure this is idempotent, in case an error occurs this function may be called multiple times!
|
|
|
|
wr := bytes.NewBuffer(buf[:0])
|
|
|
|
_, cerr := io.Copy(wr, rd)
|
|
|
|
if cerr != nil {
|
|
|
|
return cerr
|
|
|
|
}
|
|
|
|
buf = wr.Bytes()
|
2021-09-19 17:58:47 +00:00
|
|
|
|
|
|
|
// retry loading damaged data only once. If a file fails to download correctly
|
|
|
|
// the second time, then it is likely corrupted at the backend. Return the data
|
|
|
|
// to the caller in that case to let it decide what to do with the data.
|
|
|
|
if !retriedInvalidData && h.Type != restic.ConfigFile {
|
|
|
|
id, err := restic.ParseID(h.Name)
|
|
|
|
if err == nil && !restic.Hash(buf).Equal(id) {
|
|
|
|
debug.Log("retry loading broken blob %v", h)
|
|
|
|
retriedInvalidData = true
|
|
|
|
return errors.Errorf("loadAll(%v): invalid data returned", h)
|
|
|
|
}
|
|
|
|
}
|
2019-03-24 20:59:14 +00:00
|
|
|
return nil
|
2018-01-17 04:59:16 +00:00
|
|
|
})
|
2019-03-24 20:59:14 +00:00
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return buf, nil
|
2016-01-23 22:41:55 +00:00
|
|
|
}
|
2017-01-22 21:01:12 +00:00
|
|
|
|
|
|
|
// LimitedReadCloser wraps io.LimitedReader and exposes the Close() method.
|
|
|
|
type LimitedReadCloser struct {
|
2020-05-26 11:35:07 +00:00
|
|
|
io.Closer
|
|
|
|
io.LimitedReader
|
2017-01-22 21:01:12 +00:00
|
|
|
}
|
|
|
|
|
2020-05-26 11:35:07 +00:00
|
|
|
// LimitReadCloser returns a new reader wraps r in an io.LimitedReader, but also
|
2017-01-22 21:01:12 +00:00
|
|
|
// exposes the Close() method.
|
|
|
|
func LimitReadCloser(r io.ReadCloser, n int64) *LimitedReadCloser {
|
2020-05-26 11:35:07 +00:00
|
|
|
return &LimitedReadCloser{Closer: r, LimitedReader: io.LimitedReader{R: r, N: n}}
|
2017-01-22 21:01:12 +00:00
|
|
|
}
|
2018-01-17 04:59:16 +00:00
|
|
|
|
|
|
|
// DefaultLoad implements Backend.Load using lower-level openReader func
|
|
|
|
func DefaultLoad(ctx context.Context, h restic.Handle, length int, offset int64,
|
|
|
|
openReader func(ctx context.Context, h restic.Handle, length int, offset int64) (io.ReadCloser, error),
|
|
|
|
fn func(rd io.Reader) error) error {
|
|
|
|
rd, err := openReader(ctx, h, length, offset)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
err = fn(rd)
|
|
|
|
if err != nil {
|
2021-01-30 18:35:46 +00:00
|
|
|
_ = rd.Close() // ignore secondary errors closing the reader
|
2018-01-17 04:59:16 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
return rd.Close()
|
|
|
|
}
|
2021-11-06 00:14:24 +00:00
|
|
|
|
|
|
|
type memorizedLister struct {
|
|
|
|
fileInfos []restic.FileInfo
|
|
|
|
tpe restic.FileType
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *memorizedLister) List(ctx context.Context, t restic.FileType, fn func(restic.FileInfo) error) error {
|
|
|
|
if t != m.tpe {
|
|
|
|
return fmt.Errorf("filetype mismatch, expected %s got %s", m.tpe, t)
|
|
|
|
}
|
|
|
|
for _, fi := range m.fileInfos {
|
|
|
|
if ctx.Err() != nil {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
err := fn(fi)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return ctx.Err()
|
|
|
|
}
|
|
|
|
|
|
|
|
func MemorizeList(ctx context.Context, be restic.Lister, t restic.FileType) (restic.Lister, error) {
|
2021-11-06 00:23:12 +00:00
|
|
|
if _, ok := be.(*memorizedLister); ok {
|
|
|
|
return be, nil
|
|
|
|
}
|
|
|
|
|
2021-11-06 00:14:24 +00:00
|
|
|
var fileInfos []restic.FileInfo
|
|
|
|
err := be.List(ctx, t, func(fi restic.FileInfo) error {
|
|
|
|
fileInfos = append(fileInfos, fi)
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return &memorizedLister{
|
|
|
|
fileInfos: fileInfos,
|
|
|
|
tpe: t,
|
|
|
|
}, nil
|
|
|
|
}
|