From 8bfc2519d7ce10133d80923cfe7b6f0f5a3794da Mon Sep 17 00:00:00 2001 From: Michael Eischer Date: Fri, 7 Apr 2023 21:29:01 +0200 Subject: [PATCH] backend: Deduplicate sanity checks for parameters of Load() method The check is now handled by backend.DefaultLoad. This also guarantees consistent behavior across all backends. --- internal/backend/azure/azure.go | 11 ----------- internal/backend/b2/b2.go | 11 ----------- internal/backend/gs/gs.go | 10 ---------- internal/backend/local/local.go | 7 ------- internal/backend/mem/mem_backend.go | 3 --- internal/backend/s3/s3.go | 11 ----------- internal/backend/sftp/sftp.go | 7 ------- internal/backend/swift/swift.go | 11 ----------- internal/backend/utils.go | 10 ++++++++++ 9 files changed, 10 insertions(+), 71 deletions(-) diff --git a/internal/backend/azure/azure.go b/internal/backend/azure/azure.go index c92fa3f89..8a252fc81 100644 --- a/internal/backend/azure/azure.go +++ b/internal/backend/azure/azure.go @@ -300,17 +300,6 @@ func (be *Backend) Load(ctx context.Context, h restic.Handle, length int, offset func (be *Backend) openReader(ctx context.Context, h restic.Handle, length int, offset int64) (io.ReadCloser, error) { debug.Log("Load %v, length %v, offset %v from %v", h, length, offset, be.Filename(h)) - if err := h.Valid(); err != nil { - return nil, backoff.Permanent(err) - } - - if offset < 0 { - return nil, errors.New("offset is negative") - } - - if length < 0 { - return nil, errors.Errorf("invalid length %d", length) - } objName := be.Filename(h) blockBlobClient := be.container.NewBlobClient(objName) diff --git a/internal/backend/b2/b2.go b/internal/backend/b2/b2.go index 40dbbf893..b30d1eeab 100644 --- a/internal/backend/b2/b2.go +++ b/internal/backend/b2/b2.go @@ -207,17 +207,6 @@ func (be *b2Backend) Load(ctx context.Context, h restic.Handle, length int, offs func (be *b2Backend) openReader(ctx context.Context, h restic.Handle, length int, offset int64) (io.ReadCloser, error) { debug.Log("Load %v, length %v, offset %v from %v", h, length, offset, be.Filename(h)) - if err := h.Valid(); err != nil { - return nil, backoff.Permanent(err) - } - - if offset < 0 { - return nil, errors.New("offset is negative") - } - - if length < 0 { - return nil, errors.Errorf("invalid length %d", length) - } ctx, cancel := context.WithCancel(ctx) diff --git a/internal/backend/gs/gs.go b/internal/backend/gs/gs.go index 77cbcda97..ee3c30e70 100644 --- a/internal/backend/gs/gs.go +++ b/internal/backend/gs/gs.go @@ -273,17 +273,7 @@ func (be *Backend) Load(ctx context.Context, h restic.Handle, length int, offset func (be *Backend) openReader(ctx context.Context, h restic.Handle, length int, offset int64) (io.ReadCloser, error) { debug.Log("Load %v, length %v, offset %v from %v", h, length, offset, be.Filename(h)) - if err := h.Valid(); err != nil { - return nil, err - } - if offset < 0 { - return nil, errors.New("offset is negative") - } - - if length < 0 { - return nil, errors.Errorf("invalid length %d", length) - } if length == 0 { // negative length indicates read till end to GCS lib length = -1 diff --git a/internal/backend/local/local.go b/internal/backend/local/local.go index 1716e0f07..f514647a6 100644 --- a/internal/backend/local/local.go +++ b/internal/backend/local/local.go @@ -218,13 +218,6 @@ func (b *Local) Load(ctx context.Context, h restic.Handle, length int, offset in func (b *Local) openReader(ctx context.Context, h restic.Handle, length int, offset int64) (io.ReadCloser, error) { debug.Log("Load %v, length %v, offset %v", h, length, offset) - if err := h.Valid(); err != nil { - return nil, backoff.Permanent(err) - } - - if offset < 0 { - return nil, errors.New("offset is negative") - } b.sem.GetToken() f, err := fs.Open(b.Filename(h)) diff --git a/internal/backend/mem/mem_backend.go b/internal/backend/mem/mem_backend.go index 0c46dcd6e..dbdbf1c46 100644 --- a/internal/backend/mem/mem_backend.go +++ b/internal/backend/mem/mem_backend.go @@ -114,9 +114,6 @@ func (be *MemoryBackend) Load(ctx context.Context, h restic.Handle, length int, } func (be *MemoryBackend) openReader(ctx context.Context, h restic.Handle, length int, offset int64) (io.ReadCloser, error) { - if err := h.Valid(); err != nil { - return nil, backoff.Permanent(err) - } be.sem.GetToken() be.m.Lock() diff --git a/internal/backend/s3/s3.go b/internal/backend/s3/s3.go index ad652a206..91643f909 100644 --- a/internal/backend/s3/s3.go +++ b/internal/backend/s3/s3.go @@ -312,17 +312,6 @@ func (be *Backend) Load(ctx context.Context, h restic.Handle, length int, offset func (be *Backend) openReader(ctx context.Context, h restic.Handle, length int, offset int64) (io.ReadCloser, error) { debug.Log("Load %v, length %v, offset %v from %v", h, length, offset, be.Filename(h)) - if err := h.Valid(); err != nil { - return nil, backoff.Permanent(err) - } - - if offset < 0 { - return nil, errors.New("offset is negative") - } - - if length < 0 { - return nil, errors.Errorf("invalid length %d", length) - } objName := be.Filename(h) opts := minio.GetObjectOptions{} diff --git a/internal/backend/sftp/sftp.go b/internal/backend/sftp/sftp.go index 514dd58da..5d5aa90d0 100644 --- a/internal/backend/sftp/sftp.go +++ b/internal/backend/sftp/sftp.go @@ -430,13 +430,6 @@ func (wr *wrapReader) Close() error { func (r *SFTP) openReader(ctx context.Context, h restic.Handle, length int, offset int64) (io.ReadCloser, error) { debug.Log("Load %v, length %v, offset %v", h, length, offset) - if err := h.Valid(); err != nil { - return nil, backoff.Permanent(err) - } - - if offset < 0 { - return nil, errors.New("offset is negative") - } r.sem.GetToken() f, err := r.c.Open(r.Filename(h)) diff --git a/internal/backend/swift/swift.go b/internal/backend/swift/swift.go index 764c7bb62..8685b336f 100644 --- a/internal/backend/swift/swift.go +++ b/internal/backend/swift/swift.go @@ -144,17 +144,6 @@ func (be *beSwift) Load(ctx context.Context, h restic.Handle, length int, offset func (be *beSwift) openReader(ctx context.Context, h restic.Handle, length int, offset int64) (io.ReadCloser, error) { debug.Log("Load %v, length %v, offset %v", h, length, offset) - if err := h.Valid(); err != nil { - return nil, backoff.Permanent(err) - } - - if offset < 0 { - return nil, errors.New("offset is negative") - } - - if length < 0 { - return nil, errors.Errorf("invalid length %d", length) - } objName := be.Filename(h) diff --git a/internal/backend/utils.go b/internal/backend/utils.go index d2ac44670..1c1607e04 100644 --- a/internal/backend/utils.go +++ b/internal/backend/utils.go @@ -6,6 +6,7 @@ import ( "fmt" "io" + "github.com/cenkalti/backoff/v4" "github.com/restic/restic/internal/debug" "github.com/restic/restic/internal/errors" "github.com/restic/restic/internal/restic" @@ -62,6 +63,15 @@ func LimitReadCloser(r io.ReadCloser, n int64) *LimitedReadCloser { func DefaultLoad(ctx context.Context, h restic.Handle, length int, offset int64, openReader func(ctx context.Context, h restic.Handle, length int, offset int64) (io.ReadCloser, error), fn func(rd io.Reader) error) error { + if err := h.Valid(); err != nil { + return backoff.Permanent(err) + } + if offset < 0 { + return errors.New("offset is negative") + } + if length < 0 { + return errors.Errorf("invalid length %d", length) + } rd, err := openReader(ctx, h, length, offset) if err != nil { return err