mirror of
https://github.com/restic/restic.git
synced 2025-10-09 07:33:53 +00:00
backend: extract readerat from restic package
This commit is contained in:
44
internal/backend/readerat.go
Normal file
44
internal/backend/readerat.go
Normal file
@@ -0,0 +1,44 @@
|
||||
package backend
|
||||
|
||||
import (
|
||||
"context"
|
||||
"io"
|
||||
|
||||
"github.com/restic/restic/internal/debug"
|
||||
"github.com/restic/restic/internal/errors"
|
||||
"github.com/restic/restic/internal/restic"
|
||||
)
|
||||
|
||||
type backendReaderAt struct {
|
||||
ctx context.Context
|
||||
be restic.Backend
|
||||
h restic.Handle
|
||||
}
|
||||
|
||||
func (brd backendReaderAt) ReadAt(p []byte, offset int64) (n int, err error) {
|
||||
return ReadAt(brd.ctx, brd.be, brd.h, offset, p)
|
||||
}
|
||||
|
||||
// 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}
|
||||
}
|
||||
|
||||
// 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) {
|
||||
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)
|
||||
}
|
||||
|
||||
debug.Log("ReadAt(%v) ReadFull returned %v bytes", h, n)
|
||||
return n, nil
|
||||
}
|
Reference in New Issue
Block a user