mirror of
https://github.com/octoleo/restic.git
synced 2024-11-22 12:55:18 +00:00
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.
This commit is contained in:
parent
bdcafbc11c
commit
8bfc2519d7
@ -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) {
|
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))
|
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)
|
objName := be.Filename(h)
|
||||||
blockBlobClient := be.container.NewBlobClient(objName)
|
blockBlobClient := be.container.NewBlobClient(objName)
|
||||||
|
@ -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) {
|
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))
|
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)
|
ctx, cancel := context.WithCancel(ctx)
|
||||||
|
|
||||||
|
@ -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) {
|
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))
|
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 {
|
if length == 0 {
|
||||||
// negative length indicates read till end to GCS lib
|
// negative length indicates read till end to GCS lib
|
||||||
length = -1
|
length = -1
|
||||||
|
@ -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) {
|
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)
|
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()
|
b.sem.GetToken()
|
||||||
f, err := fs.Open(b.Filename(h))
|
f, err := fs.Open(b.Filename(h))
|
||||||
|
@ -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) {
|
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.sem.GetToken()
|
||||||
be.m.Lock()
|
be.m.Lock()
|
||||||
|
@ -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) {
|
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))
|
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)
|
objName := be.Filename(h)
|
||||||
opts := minio.GetObjectOptions{}
|
opts := minio.GetObjectOptions{}
|
||||||
|
@ -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) {
|
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)
|
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()
|
r.sem.GetToken()
|
||||||
f, err := r.c.Open(r.Filename(h))
|
f, err := r.c.Open(r.Filename(h))
|
||||||
|
@ -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) {
|
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)
|
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)
|
objName := be.Filename(h)
|
||||||
|
|
||||||
|
@ -6,6 +6,7 @@ import (
|
|||||||
"fmt"
|
"fmt"
|
||||||
"io"
|
"io"
|
||||||
|
|
||||||
|
"github.com/cenkalti/backoff/v4"
|
||||||
"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"
|
"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,
|
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),
|
openReader func(ctx context.Context, h restic.Handle, length int, offset int64) (io.ReadCloser, error),
|
||||||
fn func(rd io.Reader) error) 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)
|
rd, err := openReader(ctx, h, length, offset)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
|
Loading…
Reference in New Issue
Block a user