2
2
mirror of https://github.com/octoleo/restic.git synced 2024-11-30 00:33:57 +00:00
restic/internal/backend/readerat.go

45 lines
1.2 KiB
Go
Raw Normal View History

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"
"github.com/restic/restic/internal/restic"
2016-08-25 19:49:00 +00:00
)
type backendReaderAt struct {
ctx context.Context
be restic.Backend
h restic.Handle
2016-08-25 19:49:00 +00:00
}
func (brd backendReaderAt) ReadAt(p []byte, offset int64) (n int, err error) {
return ReadAt(brd.ctx, 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. The returned reader
// should not escape the caller function to avoid unexpected interactions with the
// embedded context
func ReaderAt(ctx context.Context, be restic.Backend, h restic.Handle) io.ReaderAt {
return backendReaderAt{ctx: ctx, be: be, h: h}
2016-08-25 19:49:00 +00:00
}
// ReadAt reads from the backend handle h at the given position.
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))
err = be.Load(ctx, h, len(p), offset, func(rd io.Reader) (ierr error) {
n, ierr = io.ReadFull(rd, p)
return ierr
})
if err != nil {
return 0, errors.Wrapf(err, "ReadFull(%v)", h)
}
2017-01-24 08:03:33 +00:00
debug.Log("ReadAt(%v) ReadFull returned %v bytes", h, n)
return n, nil
}