2022-07-17 11:47:54 +00:00
|
|
|
package backend
|
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"
|
2017-09-24 21:10:26 +00:00
|
|
|
"github.com/restic/restic/internal/errors"
|
2022-07-17 11:47:54 +00:00
|
|
|
"github.com/restic/restic/internal/restic"
|
2016-08-25 19:49:00 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type backendReaderAt struct {
|
2020-10-06 13:59:00 +00:00
|
|
|
ctx context.Context
|
2022-07-17 11:47:54 +00:00
|
|
|
be restic.Backend
|
|
|
|
h restic.Handle
|
2016-08-25 19:49:00 +00:00
|
|
|
}
|
|
|
|
|
2017-01-23 16:04:23 +00:00
|
|
|
func (brd backendReaderAt) ReadAt(p []byte, offset int64) (n int, err error) {
|
2020-10-06 13:59:00 +00:00
|
|
|
return ReadAt(brd.ctx, brd.be, brd.h, offset, p)
|
2016-08-25 19:49:00 +00:00
|
|
|
}
|
|
|
|
|
2020-10-06 13:59:00 +00:00
|
|
|
// ReaderAt returns an io.ReaderAt for a file in the backend. The returned reader
|
|
|
|
// should not escape the caller function to avoid unexpected interactions with the
|
|
|
|
// embedded context
|
2022-07-17 11:47:54 +00:00
|
|
|
func ReaderAt(ctx context.Context, be restic.Backend, h restic.Handle) io.ReaderAt {
|
2020-10-06 13:59:00 +00:00
|
|
|
return backendReaderAt{ctx: ctx, be: be, h: h}
|
2016-08-25 19:49:00 +00:00
|
|
|
}
|
2017-01-23 16:04:23 +00:00
|
|
|
|
|
|
|
// ReadAt reads from the backend handle h at the given position.
|
2022-07-17 11:47:54 +00:00
|
|
|
func ReadAt(ctx context.Context, be restic.Backend, h restic.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))
|
2018-01-17 04:59:16 +00:00
|
|
|
|
|
|
|
err = be.Load(ctx, h, len(p), offset, func(rd io.Reader) (ierr error) {
|
|
|
|
n, ierr = io.ReadFull(rd, p)
|
|
|
|
|
|
|
|
return ierr
|
|
|
|
})
|
2017-01-23 16:04:23 +00:00
|
|
|
if err != nil {
|
2021-05-15 21:11:48 +00:00
|
|
|
return 0, errors.Wrapf(err, "ReadFull(%v)", h)
|
2017-01-23 16:04:23 +00:00
|
|
|
}
|
|
|
|
|
2017-01-24 08:03:33 +00:00
|
|
|
debug.Log("ReadAt(%v) ReadFull returned %v bytes", h, n)
|
2021-05-15 21:11:48 +00:00
|
|
|
return n, nil
|
2017-01-23 16:04:23 +00:00
|
|
|
}
|