2016-08-31 20:39:36 +00:00
|
|
|
package restic
|
2016-08-25 19:49:00 +00:00
|
|
|
|
|
|
|
import (
|
2017-06-03 15:39:57 +00:00
|
|
|
"context"
|
2016-08-25 19:49:00 +00:00
|
|
|
"io"
|
2017-07-23 12:21:03 +00:00
|
|
|
|
|
|
|
"github.com/restic/restic/internal/debug"
|
2016-08-25 19:49:00 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type backendReaderAt struct {
|
|
|
|
be Backend
|
|
|
|
h Handle
|
|
|
|
}
|
|
|
|
|
2017-01-23 16:04:23 +00:00
|
|
|
func (brd backendReaderAt) ReadAt(p []byte, offset int64) (n int, err error) {
|
2017-06-04 09:16:55 +00:00
|
|
|
return ReadAt(context.TODO(), brd.be, brd.h, offset, p)
|
2016-08-25 19:49:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// ReaderAt returns an io.ReaderAt for a file in the backend.
|
|
|
|
func ReaderAt(be Backend, h Handle) io.ReaderAt {
|
|
|
|
return backendReaderAt{be: be, h: h}
|
|
|
|
}
|
2017-01-23 16:04:23 +00:00
|
|
|
|
|
|
|
// ReadAt reads from the backend handle h at the given position.
|
2017-06-04 09:16:55 +00:00
|
|
|
func ReadAt(ctx context.Context, be Backend, h Handle, offset int64, p []byte) (n int, err error) {
|
2017-01-24 08:03:33 +00:00
|
|
|
debug.Log("ReadAt(%v) at %v, len %v", h, offset, len(p))
|
2017-06-04 09:16:55 +00:00
|
|
|
rd, err := be.Load(ctx, h, len(p), offset)
|
2017-01-23 16:04:23 +00:00
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
|
|
|
|
n, err = io.ReadFull(rd, p)
|
|
|
|
e := rd.Close()
|
|
|
|
if err == nil {
|
|
|
|
err = e
|
|
|
|
}
|
|
|
|
|
2017-01-24 08:03:33 +00:00
|
|
|
debug.Log("ReadAt(%v) ReadFull returned %v bytes", h, n)
|
|
|
|
|
2017-01-23 16:04:23 +00:00
|
|
|
return n, err
|
|
|
|
}
|