2016-01-23 22:41:55 +00:00
|
|
|
package backend
|
|
|
|
|
2016-08-29 17:18:57 +00:00
|
|
|
import (
|
2017-06-03 15:39:57 +00:00
|
|
|
"context"
|
2016-08-29 17:18:57 +00:00
|
|
|
"io"
|
2017-01-23 15:07:39 +00:00
|
|
|
"io/ioutil"
|
2016-08-31 20:39:36 +00:00
|
|
|
"restic"
|
2016-08-29 17:18:57 +00:00
|
|
|
)
|
2016-02-04 18:37:33 +00:00
|
|
|
|
2017-01-23 15:07:39 +00:00
|
|
|
// LoadAll reads all data stored in the backend for the handle.
|
2017-06-03 15:39:57 +00:00
|
|
|
func LoadAll(ctx context.Context, be restic.Backend, h restic.Handle) (buf []byte, err error) {
|
|
|
|
rd, err := be.Load(ctx, h, 0, 0)
|
2016-01-23 22:41:55 +00:00
|
|
|
if err != nil {
|
2017-01-23 15:07:39 +00:00
|
|
|
return nil, err
|
2016-01-23 22:41:55 +00:00
|
|
|
}
|
|
|
|
|
2017-01-23 15:07:39 +00:00
|
|
|
defer func() {
|
2017-05-01 13:38:01 +00:00
|
|
|
_, e := io.Copy(ioutil.Discard, rd)
|
|
|
|
if err == nil {
|
|
|
|
err = e
|
|
|
|
}
|
|
|
|
|
|
|
|
e = rd.Close()
|
2017-01-23 15:07:39 +00:00
|
|
|
if err == nil {
|
|
|
|
err = e
|
|
|
|
}
|
|
|
|
}()
|
2016-01-23 22:41:55 +00:00
|
|
|
|
2017-01-23 15:07:39 +00:00
|
|
|
return ioutil.ReadAll(rd)
|
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 {
|
|
|
|
io.ReadCloser
|
|
|
|
io.Reader
|
|
|
|
}
|
|
|
|
|
|
|
|
// Read reads data from the limited reader.
|
|
|
|
func (l *LimitedReadCloser) Read(p []byte) (int, error) {
|
|
|
|
return l.Reader.Read(p)
|
|
|
|
}
|
|
|
|
|
|
|
|
// LimitReadCloser returns a new reader wraps r in an io.LimitReader, but also
|
|
|
|
// exposes the Close() method.
|
|
|
|
func LimitReadCloser(r io.ReadCloser, n int64) *LimitedReadCloser {
|
|
|
|
return &LimitedReadCloser{ReadCloser: r, Reader: io.LimitReader(r, n)}
|
|
|
|
}
|