backend: extract readerat from restic package

This commit is contained in:
Michael Eischer 2022-07-17 13:47:54 +02:00
parent c44b21d366
commit 9729e6d7ef
4 changed files with 14 additions and 11 deletions

View File

@ -107,7 +107,7 @@ func printPacks(ctx context.Context, repo *repository.Repository, wr io.Writer)
return repo.List(ctx, restic.PackFile, func(id restic.ID, size int64) error { return repo.List(ctx, restic.PackFile, func(id restic.ID, size int64) error {
h := restic.Handle{Type: restic.PackFile, Name: id.String()} h := restic.Handle{Type: restic.PackFile, Name: id.String()}
blobs, _, err := pack.List(repo.Key(), restic.ReaderAt(ctx, repo.Backend(), h), size) blobs, _, err := pack.List(repo.Key(), backend.ReaderAt(ctx, repo.Backend(), h), size)
if err != nil { if err != nil {
Warnf("error for pack %v: %v\n", id.Str(), err) Warnf("error for pack %v: %v\n", id.Str(), err)
return nil return nil
@ -525,7 +525,7 @@ func examinePack(ctx context.Context, repo restic.Repository, id restic.ID) erro
Printf(" ========================================\n") Printf(" ========================================\n")
Printf(" inspect the pack itself\n") Printf(" inspect the pack itself\n")
blobs, _, err := pack.List(repo.Key(), restic.ReaderAt(ctx, repo.Backend(), h), fi.Size) blobs, _, err := pack.List(repo.Key(), backend.ReaderAt(ctx, repo.Backend(), h), fi.Size)
if err != nil { if err != nil {
return fmt.Errorf("pack %v: %v", id.Str(), err) return fmt.Errorf("pack %v: %v", id.Str(), err)
} }

View File

@ -1,4 +1,4 @@
package restic package backend
import ( import (
"context" "context"
@ -6,12 +6,13 @@ import (
"github.com/restic/restic/internal/debug" "github.com/restic/restic/internal/debug"
"github.com/restic/restic/internal/errors" "github.com/restic/restic/internal/errors"
"github.com/restic/restic/internal/restic"
) )
type backendReaderAt struct { type backendReaderAt struct {
ctx context.Context ctx context.Context
be Backend be restic.Backend
h Handle h restic.Handle
} }
func (brd backendReaderAt) ReadAt(p []byte, offset int64) (n int, err error) { func (brd backendReaderAt) ReadAt(p []byte, offset int64) (n int, err error) {
@ -21,12 +22,12 @@ func (brd backendReaderAt) ReadAt(p []byte, offset int64) (n int, err error) {
// ReaderAt returns an io.ReaderAt for a file in the backend. The returned reader // 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 // should not escape the caller function to avoid unexpected interactions with the
// embedded context // embedded context
func ReaderAt(ctx context.Context, be Backend, h Handle) io.ReaderAt { func ReaderAt(ctx context.Context, be restic.Backend, h restic.Handle) io.ReaderAt {
return backendReaderAt{ctx: ctx, be: be, h: h} return backendReaderAt{ctx: ctx, be: be, h: h}
} }
// ReadAt reads from the backend handle h at the given position. // ReadAt reads from the backend handle h at the given position.
func ReadAt(ctx context.Context, be Backend, h Handle, offset int64, p []byte) (n int, err error) { 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)) 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) { err = be.Load(ctx, h, len(p), offset, func(rd io.Reader) (ierr error) {

View File

@ -9,6 +9,7 @@ import (
"io" "io"
"testing" "testing"
"github.com/restic/restic/internal/backend"
"github.com/restic/restic/internal/backend/mem" "github.com/restic/restic/internal/backend/mem"
"github.com/restic/restic/internal/crypto" "github.com/restic/restic/internal/crypto"
"github.com/restic/restic/internal/pack" "github.com/restic/restic/internal/pack"
@ -128,7 +129,7 @@ func TestUnpackReadSeeker(t *testing.T) {
handle := restic.Handle{Type: restic.PackFile, Name: id.String()} handle := restic.Handle{Type: restic.PackFile, Name: id.String()}
rtest.OK(t, b.Save(context.TODO(), handle, restic.NewByteReader(packData, b.Hasher()))) rtest.OK(t, b.Save(context.TODO(), handle, restic.NewByteReader(packData, b.Hasher())))
verifyBlobs(t, bufs, k, restic.ReaderAt(context.TODO(), b, handle), packSize) verifyBlobs(t, bufs, k, backend.ReaderAt(context.TODO(), b, handle), packSize)
} }
func TestShortPack(t *testing.T) { func TestShortPack(t *testing.T) {
@ -141,5 +142,5 @@ func TestShortPack(t *testing.T) {
handle := restic.Handle{Type: restic.PackFile, Name: id.String()} handle := restic.Handle{Type: restic.PackFile, Name: id.String()}
rtest.OK(t, b.Save(context.TODO(), handle, restic.NewByteReader(packData, b.Hasher()))) rtest.OK(t, b.Save(context.TODO(), handle, restic.NewByteReader(packData, b.Hasher())))
verifyBlobs(t, bufs, k, restic.ReaderAt(context.TODO(), b, handle), packSize) verifyBlobs(t, bufs, k, backend.ReaderAt(context.TODO(), b, handle), packSize)
} }

View File

@ -14,6 +14,7 @@ import (
"github.com/cenkalti/backoff/v4" "github.com/cenkalti/backoff/v4"
"github.com/klauspost/compress/zstd" "github.com/klauspost/compress/zstd"
"github.com/restic/chunker" "github.com/restic/chunker"
"github.com/restic/restic/internal/backend"
"github.com/restic/restic/internal/backend/dryrun" "github.com/restic/restic/internal/backend/dryrun"
"github.com/restic/restic/internal/cache" "github.com/restic/restic/internal/cache"
"github.com/restic/restic/internal/crypto" "github.com/restic/restic/internal/crypto"
@ -263,7 +264,7 @@ func (r *Repository) LoadBlob(ctx context.Context, t restic.BlobType, id restic.
buf = buf[:blob.Length] buf = buf[:blob.Length]
} }
n, err := restic.ReadAt(ctx, r.be, h, int64(blob.Offset), buf) n, err := backend.ReadAt(ctx, r.be, h, int64(blob.Offset), buf)
if err != nil { if err != nil {
debug.Log("error loading blob %v: %v", blob, err) debug.Log("error loading blob %v: %v", blob, err)
lastError = err lastError = err
@ -793,7 +794,7 @@ func (r *Repository) List(ctx context.Context, t restic.FileType, fn func(restic
func (r *Repository) ListPack(ctx context.Context, id restic.ID, size int64) ([]restic.Blob, uint32, error) { func (r *Repository) ListPack(ctx context.Context, id restic.ID, size int64) ([]restic.Blob, uint32, error) {
h := restic.Handle{Type: restic.PackFile, Name: id.String()} h := restic.Handle{Type: restic.PackFile, Name: id.String()}
return pack.List(r.Key(), restic.ReaderAt(ctx, r.Backend(), h), size) return pack.List(r.Key(), backend.ReaderAt(ctx, r.Backend(), h), size)
} }
// Delete calls backend.Delete() if implemented, and returns an error // Delete calls backend.Delete() if implemented, and returns an error