Merge pull request #3970 from MichaelEischer/split-retry-backend

Split backend package into smaller parts
This commit is contained in:
Michael Eischer 2022-10-21 21:49:46 +02:00 committed by GitHub
commit 2e9ee8577a
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
21 changed files with 72 additions and 167 deletions

View file

@ -22,6 +22,7 @@ import (
"github.com/restic/restic/internal/backend/location" "github.com/restic/restic/internal/backend/location"
"github.com/restic/restic/internal/backend/rclone" "github.com/restic/restic/internal/backend/rclone"
"github.com/restic/restic/internal/backend/rest" "github.com/restic/restic/internal/backend/rest"
"github.com/restic/restic/internal/backend/retry"
"github.com/restic/restic/internal/backend/s3" "github.com/restic/restic/internal/backend/s3"
"github.com/restic/restic/internal/backend/sftp" "github.com/restic/restic/internal/backend/sftp"
"github.com/restic/restic/internal/backend/swift" "github.com/restic/restic/internal/backend/swift"
@ -445,7 +446,7 @@ func OpenRepository(ctx context.Context, opts GlobalOptions) (*repository.Reposi
success := func(msg string, retries int) { success := func(msg string, retries int) {
Warnf("%v operation successful after %d retries\n", msg, retries) Warnf("%v operation successful after %d retries\n", msg, retries)
} }
be = backend.NewRetryBackend(be, 10, report, success) be = retry.New(be, 10, report, success)
// wrap backend if a test specified a hook // wrap backend if a test specified a hook
if opts.backendTestHook != nil { if opts.backendTestHook != nil {

View file

@ -9,7 +9,7 @@ import (
"runtime" "runtime"
"testing" "testing"
"github.com/restic/restic/internal/backend" "github.com/restic/restic/internal/backend/retry"
"github.com/restic/restic/internal/options" "github.com/restic/restic/internal/options"
"github.com/restic/restic/internal/repository" "github.com/restic/restic/internal/repository"
"github.com/restic/restic/internal/restic" "github.com/restic/restic/internal/restic"
@ -172,7 +172,7 @@ func withTestEnvironment(t testing.TB) (env *testEnvironment, cleanup func()) {
repository.TestUseLowSecurityKDFParameters(t) repository.TestUseLowSecurityKDFParameters(t)
restic.TestDisableCheckPolynomial(t) restic.TestDisableCheckPolynomial(t)
backend.TestFastRetries(t) retry.TestFastRetries(t)
tempdir, err := ioutil.TempDir(rtest.TestTempDir, "restic-test-") tempdir, err := ioutil.TempDir(rtest.TestTempDir, "restic-test-")
rtest.OK(t, err) rtest.OK(t, err)

View file

@ -13,6 +13,7 @@ import (
"strings" "strings"
"github.com/restic/restic/internal/backend" "github.com/restic/restic/internal/backend"
"github.com/restic/restic/internal/backend/layout"
"github.com/restic/restic/internal/backend/sema" "github.com/restic/restic/internal/backend/sema"
"github.com/restic/restic/internal/debug" "github.com/restic/restic/internal/debug"
"github.com/restic/restic/internal/errors" "github.com/restic/restic/internal/errors"
@ -30,7 +31,7 @@ type Backend struct {
sem sema.Semaphore sem sema.Semaphore
prefix string prefix string
listMaxItems int listMaxItems int
backend.Layout layout.Layout
} }
const defaultListMaxItems = 5000 const defaultListMaxItems = 5000
@ -85,7 +86,7 @@ func open(cfg Config, rt http.RoundTripper) (*Backend, error) {
connections: cfg.Connections, connections: cfg.Connections,
sem: sem, sem: sem,
prefix: cfg.Prefix, prefix: cfg.Prefix,
Layout: &backend.DefaultLayout{ Layout: &layout.DefaultLayout{
Path: cfg.Prefix, Path: cfg.Prefix,
Join: path.Join, Join: path.Join,
}, },

View file

@ -10,6 +10,7 @@ import (
"time" "time"
"github.com/restic/restic/internal/backend" "github.com/restic/restic/internal/backend"
"github.com/restic/restic/internal/backend/layout"
"github.com/restic/restic/internal/backend/sema" "github.com/restic/restic/internal/backend/sema"
"github.com/restic/restic/internal/debug" "github.com/restic/restic/internal/debug"
"github.com/restic/restic/internal/errors" "github.com/restic/restic/internal/errors"
@ -25,7 +26,7 @@ type b2Backend struct {
bucket *b2.Bucket bucket *b2.Bucket
cfg Config cfg Config
listMaxItems int listMaxItems int
backend.Layout layout.Layout
sem sema.Semaphore sem sema.Semaphore
} }
@ -97,7 +98,7 @@ func Open(ctx context.Context, cfg Config, rt http.RoundTripper) (restic.Backend
client: client, client: client,
bucket: bucket, bucket: bucket,
cfg: cfg, cfg: cfg,
Layout: &backend.DefaultLayout{ Layout: &layout.DefaultLayout{
Join: path.Join, Join: path.Join,
Path: cfg.Prefix, Path: cfg.Prefix,
}, },
@ -138,7 +139,7 @@ func Create(ctx context.Context, cfg Config, rt http.RoundTripper) (restic.Backe
client: client, client: client,
bucket: bucket, bucket: bucket,
cfg: cfg, cfg: cfg,
Layout: &backend.DefaultLayout{ Layout: &layout.DefaultLayout{
Join: path.Join, Join: path.Join,
Path: cfg.Prefix, Path: cfg.Prefix,
}, },

View file

@ -1,84 +0,0 @@
package backend
import (
"context"
"io"
"io/ioutil"
"math/rand"
"sync"
"github.com/restic/restic/internal/errors"
"github.com/restic/restic/internal/restic"
)
// ErrorBackend is used to induce errors into various function calls and test
// the retry functions.
type ErrorBackend struct {
FailSave float32
FailSaveRead float32
FailLoad float32
FailStat float32
restic.Backend
r *rand.Rand
m sync.Mutex
}
// statically ensure that ErrorBackend implements restic.Backend.
var _ restic.Backend = &ErrorBackend{}
// NewErrorBackend wraps be with a backend that returns errors according to
// given probabilities.
func NewErrorBackend(be restic.Backend, seed int64) *ErrorBackend {
return &ErrorBackend{
Backend: be,
r: rand.New(rand.NewSource(seed)),
}
}
func (be *ErrorBackend) fail(p float32) bool {
be.m.Lock()
v := be.r.Float32()
be.m.Unlock()
return v < p
}
// Save stores the data in the backend under the given handle.
func (be *ErrorBackend) Save(ctx context.Context, h restic.Handle, rd restic.RewindReader) error {
if be.fail(be.FailSave) {
return errors.Errorf("Save(%v) random error induced", h)
}
if be.fail(be.FailSaveRead) {
_, err := io.CopyN(ioutil.Discard, rd, be.r.Int63n(1000))
if err != nil {
return err
}
return errors.Errorf("Save(%v) random error with partial read induced", h)
}
return be.Backend.Save(ctx, h, rd)
}
// Load returns a reader that yields the contents of the file at h at the
// given offset. If length is larger than zero, only a portion of the file
// is returned. rd must be closed after use. If an error is returned, the
// ReadCloser must be nil.
func (be *ErrorBackend) Load(ctx context.Context, h restic.Handle, length int, offset int64, consumer func(rd io.Reader) error) error {
if be.fail(be.FailLoad) {
return errors.Errorf("Load(%v, %v, %v) random error induced", h, length, offset)
}
return be.Backend.Load(ctx, h, length, offset, consumer)
}
// Stat returns information about the File identified by h.
func (be *ErrorBackend) Stat(ctx context.Context, h restic.Handle) (restic.FileInfo, error) {
if be.fail(be.FailLoad) {
return restic.FileInfo{}, errors.Errorf("Stat(%v) random error induced", h)
}
return be.Stat(ctx, h)
}

View file

@ -14,6 +14,7 @@ import (
"cloud.google.com/go/storage" "cloud.google.com/go/storage"
"github.com/pkg/errors" "github.com/pkg/errors"
"github.com/restic/restic/internal/backend" "github.com/restic/restic/internal/backend"
"github.com/restic/restic/internal/backend/layout"
"github.com/restic/restic/internal/backend/sema" "github.com/restic/restic/internal/backend/sema"
"github.com/restic/restic/internal/debug" "github.com/restic/restic/internal/debug"
"github.com/restic/restic/internal/restic" "github.com/restic/restic/internal/restic"
@ -41,7 +42,7 @@ type Backend struct {
bucket *storage.BucketHandle bucket *storage.BucketHandle
prefix string prefix string
listMaxItems int listMaxItems int
backend.Layout layout.Layout
} }
// Ensure that *Backend implements restic.Backend. // Ensure that *Backend implements restic.Backend.
@ -111,7 +112,7 @@ func open(cfg Config, rt http.RoundTripper) (*Backend, error) {
bucketName: cfg.Bucket, bucketName: cfg.Bucket,
bucket: gcsClient.Bucket(cfg.Bucket), bucket: gcsClient.Bucket(cfg.Bucket),
prefix: cfg.Prefix, prefix: cfg.Prefix,
Layout: &backend.DefaultLayout{ Layout: &layout.DefaultLayout{
Path: cfg.Prefix, Path: cfg.Prefix,
Join: path.Join, Join: path.Join,
}, },

View file

@ -1,4 +1,4 @@
package backend package layout
import ( import (
"context" "context"

View file

@ -1,4 +1,4 @@
package backend package layout
import ( import (
"encoding/hex" "encoding/hex"

View file

@ -1,4 +1,4 @@
package backend package layout
import "github.com/restic/restic/internal/restic" import "github.com/restic/restic/internal/restic"

View file

@ -1,4 +1,4 @@
package backend package layout
import "github.com/restic/restic/internal/restic" import "github.com/restic/restic/internal/restic"

View file

@ -1,4 +1,4 @@
package backend package layout
import ( import (
"context" "context"
@ -362,15 +362,15 @@ func TestDetectLayout(t *testing.T) {
filename string filename string
want string want string
}{ }{
{"repo-layout-default.tar.gz", "*backend.DefaultLayout"}, {"repo-layout-default.tar.gz", "*layout.DefaultLayout"},
{"repo-layout-s3legacy.tar.gz", "*backend.S3LegacyLayout"}, {"repo-layout-s3legacy.tar.gz", "*layout.S3LegacyLayout"},
} }
var fs = &LocalFilesystem{} var fs = &LocalFilesystem{}
for _, test := range tests { for _, test := range tests {
for _, fs := range []Filesystem{fs, nil} { for _, fs := range []Filesystem{fs, nil} {
t.Run(fmt.Sprintf("%v/fs-%T", test.filename, fs), func(t *testing.T) { t.Run(fmt.Sprintf("%v/fs-%T", test.filename, fs), func(t *testing.T) {
rtest.SetupTarTestFixture(t, path, filepath.Join("testdata", test.filename)) rtest.SetupTarTestFixture(t, path, filepath.Join("../testdata", test.filename))
layout, err := DetectLayout(context.TODO(), fs, filepath.Join(path, "repo")) layout, err := DetectLayout(context.TODO(), fs, filepath.Join(path, "repo"))
if err != nil { if err != nil {
@ -401,12 +401,12 @@ func TestParseLayout(t *testing.T) {
defaultLayoutName string defaultLayoutName string
want string want string
}{ }{
{"default", "", "*backend.DefaultLayout"}, {"default", "", "*layout.DefaultLayout"},
{"s3legacy", "", "*backend.S3LegacyLayout"}, {"s3legacy", "", "*layout.S3LegacyLayout"},
{"", "", "*backend.DefaultLayout"}, {"", "", "*layout.DefaultLayout"},
} }
rtest.SetupTarTestFixture(t, path, filepath.Join("testdata", "repo-layout-default.tar.gz")) rtest.SetupTarTestFixture(t, path, filepath.Join("..", "testdata", "repo-layout-default.tar.gz"))
for _, test := range tests { for _, test := range tests {
t.Run(test.layoutName, func(t *testing.T) { t.Run(test.layoutName, func(t *testing.T) {

View file

@ -10,6 +10,7 @@ import (
"syscall" "syscall"
"github.com/restic/restic/internal/backend" "github.com/restic/restic/internal/backend"
"github.com/restic/restic/internal/backend/layout"
"github.com/restic/restic/internal/backend/sema" "github.com/restic/restic/internal/backend/sema"
"github.com/restic/restic/internal/debug" "github.com/restic/restic/internal/debug"
"github.com/restic/restic/internal/errors" "github.com/restic/restic/internal/errors"
@ -23,7 +24,7 @@ import (
type Local struct { type Local struct {
Config Config
sem sema.Semaphore sem sema.Semaphore
backend.Layout layout.Layout
backend.Modes backend.Modes
} }
@ -33,7 +34,7 @@ var _ restic.Backend = &Local{}
const defaultLayout = "default" const defaultLayout = "default"
func open(ctx context.Context, cfg Config) (*Local, error) { func open(ctx context.Context, cfg Config) (*Local, error) {
l, err := backend.ParseLayout(ctx, &backend.LocalFilesystem{}, cfg.Layout, defaultLayout, cfg.Path) l, err := layout.ParseLayout(ctx, &layout.LocalFilesystem{}, cfg.Layout, defaultLayout, cfg.Path)
if err != nil { if err != nil {
return nil, err return nil, err
} }

View file

@ -2,25 +2,6 @@ package backend
import "os" import "os"
// Paths contains the default paths for file-based backends (e.g. local).
var Paths = struct {
Data string
Snapshots string
Index string
Locks string
Keys string
Temp string
Config string
}{
"data",
"snapshots",
"index",
"locks",
"keys",
"tmp",
"config",
}
type Modes struct { type Modes struct {
Dir os.FileMode Dir os.FileMode
File os.FileMode File os.FileMode

View file

@ -14,7 +14,7 @@ import (
"strconv" "strconv"
"strings" "strings"
"github.com/restic/restic/internal/backend" "github.com/restic/restic/internal/backend/layout"
"github.com/restic/restic/internal/backend/sema" "github.com/restic/restic/internal/backend/sema"
"github.com/restic/restic/internal/debug" "github.com/restic/restic/internal/debug"
"github.com/restic/restic/internal/errors" "github.com/restic/restic/internal/errors"
@ -32,7 +32,7 @@ type Backend struct {
connections uint connections uint
sem sema.Semaphore sem sema.Semaphore
client http.Client client http.Client
backend.Layout layout.Layout
} }
// the REST API protocol version is decided by HTTP request headers, these are the constants. // the REST API protocol version is decided by HTTP request headers, these are the constants.
@ -57,7 +57,7 @@ func Open(cfg Config, rt http.RoundTripper) (*Backend, error) {
be := &Backend{ be := &Backend{
url: cfg.URL, url: cfg.URL,
client: http.Client{Transport: rt}, client: http.Client{Transport: rt},
Layout: &backend.RESTLayout{URL: url, Join: path.Join}, Layout: &layout.RESTLayout{URL: url, Join: path.Join},
connections: cfg.Connections, connections: cfg.Connections,
sem: sem, sem: sem,
} }

View file

@ -1,4 +1,4 @@
package backend package retry
import ( import (
"context" "context"
@ -11,9 +11,9 @@ import (
"github.com/restic/restic/internal/restic" "github.com/restic/restic/internal/restic"
) )
// RetryBackend retries operations on the backend in case of an error with a // Backend retries operations on the backend in case of an error with a
// backoff. // backoff.
type RetryBackend struct { type Backend struct {
restic.Backend restic.Backend
MaxTries int MaxTries int
Report func(string, error, time.Duration) Report func(string, error, time.Duration)
@ -21,14 +21,14 @@ type RetryBackend struct {
} }
// statically ensure that RetryBackend implements restic.Backend. // statically ensure that RetryBackend implements restic.Backend.
var _ restic.Backend = &RetryBackend{} var _ restic.Backend = &Backend{}
// NewRetryBackend wraps be with a backend that retries operations after a // New wraps be with a backend that retries operations after a
// backoff. report is called with a description and the error, if one occurred. // backoff. report is called with a description and the error, if one occurred.
// success is called with the number of retries before a successful operation // success is called with the number of retries before a successful operation
// (it is not called if it succeeded on the first try) // (it is not called if it succeeded on the first try)
func NewRetryBackend(be restic.Backend, maxTries int, report func(string, error, time.Duration), success func(string, int)) *RetryBackend { func New(be restic.Backend, maxTries int, report func(string, error, time.Duration), success func(string, int)) *Backend {
return &RetryBackend{ return &Backend{
Backend: be, Backend: be,
MaxTries: maxTries, MaxTries: maxTries,
Report: report, Report: report,
@ -57,7 +57,7 @@ func retryNotifyErrorWithSuccess(operation backoff.Operation, b backoff.BackOff,
var fastRetries = false var fastRetries = false
func (be *RetryBackend) retry(ctx context.Context, msg string, f func() error) error { func (be *Backend) retry(ctx context.Context, msg string, f func() error) error {
// Don't do anything when called with an already cancelled context. There would be // Don't do anything when called with an already cancelled context. There would be
// no retries in that case either, so be consistent and abort always. // no retries in that case either, so be consistent and abort always.
// This enforces a strict contract for backend methods: Using a cancelled context // This enforces a strict contract for backend methods: Using a cancelled context
@ -92,7 +92,7 @@ func (be *RetryBackend) retry(ctx context.Context, msg string, f func() error) e
} }
// Save stores the data in the backend under the given handle. // Save stores the data in the backend under the given handle.
func (be *RetryBackend) Save(ctx context.Context, h restic.Handle, rd restic.RewindReader) error { func (be *Backend) Save(ctx context.Context, h restic.Handle, rd restic.RewindReader) error {
return be.retry(ctx, fmt.Sprintf("Save(%v)", h), func() error { return be.retry(ctx, fmt.Sprintf("Save(%v)", h), func() error {
err := rd.Rewind() err := rd.Rewind()
if err != nil { if err != nil {
@ -125,7 +125,7 @@ func (be *RetryBackend) Save(ctx context.Context, h restic.Handle, rd restic.Rew
// given offset. If length is larger than zero, only a portion of the file // given offset. If length is larger than zero, only a portion of the file
// is returned. rd must be closed after use. If an error is returned, the // is returned. rd must be closed after use. If an error is returned, the
// ReadCloser must be nil. // ReadCloser must be nil.
func (be *RetryBackend) Load(ctx context.Context, h restic.Handle, length int, offset int64, consumer func(rd io.Reader) error) (err error) { func (be *Backend) Load(ctx context.Context, h restic.Handle, length int, offset int64, consumer func(rd io.Reader) error) (err error) {
return be.retry(ctx, fmt.Sprintf("Load(%v, %v, %v)", h, length, offset), return be.retry(ctx, fmt.Sprintf("Load(%v, %v, %v)", h, length, offset),
func() error { func() error {
return be.Backend.Load(ctx, h, length, offset, consumer) return be.Backend.Load(ctx, h, length, offset, consumer)
@ -133,7 +133,7 @@ func (be *RetryBackend) Load(ctx context.Context, h restic.Handle, length int, o
} }
// Stat returns information about the File identified by h. // Stat returns information about the File identified by h.
func (be *RetryBackend) Stat(ctx context.Context, h restic.Handle) (fi restic.FileInfo, err error) { func (be *Backend) Stat(ctx context.Context, h restic.Handle) (fi restic.FileInfo, err error) {
err = be.retry(ctx, fmt.Sprintf("Stat(%v)", h), err = be.retry(ctx, fmt.Sprintf("Stat(%v)", h),
func() error { func() error {
var innerError error var innerError error
@ -145,14 +145,14 @@ func (be *RetryBackend) Stat(ctx context.Context, h restic.Handle) (fi restic.Fi
} }
// Remove removes a File with type t and name. // Remove removes a File with type t and name.
func (be *RetryBackend) Remove(ctx context.Context, h restic.Handle) (err error) { func (be *Backend) Remove(ctx context.Context, h restic.Handle) (err error) {
return be.retry(ctx, fmt.Sprintf("Remove(%v)", h), func() error { return be.retry(ctx, fmt.Sprintf("Remove(%v)", h), func() error {
return be.Backend.Remove(ctx, h) return be.Backend.Remove(ctx, h)
}) })
} }
// Test a boolean value whether a File with the name and type exists. // Test a boolean value whether a File with the name and type exists.
func (be *RetryBackend) Test(ctx context.Context, h restic.Handle) (exists bool, err error) { func (be *Backend) Test(ctx context.Context, h restic.Handle) (exists bool, err error) {
err = be.retry(ctx, fmt.Sprintf("Test(%v)", h), func() error { err = be.retry(ctx, fmt.Sprintf("Test(%v)", h), func() error {
var innerError error var innerError error
exists, innerError = be.Backend.Test(ctx, h) exists, innerError = be.Backend.Test(ctx, h)
@ -166,7 +166,7 @@ func (be *RetryBackend) Test(ctx context.Context, h restic.Handle) (exists bool,
// error is returned by the underlying backend, the request is retried. When fn // error is returned by the underlying backend, the request is retried. When fn
// returns an error, the operation is aborted and the error is returned to the // returns an error, the operation is aborted and the error is returned to the
// caller. // caller.
func (be *RetryBackend) List(ctx context.Context, t restic.FileType, fn func(restic.FileInfo) error) error { func (be *Backend) List(ctx context.Context, t restic.FileType, fn func(restic.FileInfo) error) error {
// create a new context that we can cancel when fn returns an error, so // create a new context that we can cancel when fn returns an error, so
// that listing is aborted // that listing is aborted
listCtx, cancel := context.WithCancel(ctx) listCtx, cancel := context.WithCancel(ctx)

View file

@ -1,4 +1,4 @@
package backend package retry
import ( import (
"bytes" "bytes"
@ -36,7 +36,7 @@ func TestBackendSaveRetry(t *testing.T) {
} }
TestFastRetries(t) TestFastRetries(t)
retryBackend := NewRetryBackend(be, 10, nil, nil) retryBackend := New(be, 10, nil, nil)
data := test.Random(23, 5*1024*1024+11241) data := test.Random(23, 5*1024*1024+11241)
err := retryBackend.Save(context.TODO(), restic.Handle{}, restic.NewByteReader(data, be.Hasher())) err := retryBackend.Save(context.TODO(), restic.Handle{}, restic.NewByteReader(data, be.Hasher()))
@ -72,7 +72,7 @@ func TestBackendSaveRetryAtomic(t *testing.T) {
} }
TestFastRetries(t) TestFastRetries(t)
retryBackend := NewRetryBackend(be, 10, nil, nil) retryBackend := New(be, 10, nil, nil)
data := test.Random(23, 5*1024*1024+11241) data := test.Random(23, 5*1024*1024+11241)
err := retryBackend.Save(context.TODO(), restic.Handle{}, restic.NewByteReader(data, be.Hasher())) err := retryBackend.Save(context.TODO(), restic.Handle{}, restic.NewByteReader(data, be.Hasher()))
@ -106,7 +106,7 @@ func TestBackendListRetry(t *testing.T) {
} }
TestFastRetries(t) TestFastRetries(t)
retryBackend := NewRetryBackend(be, 10, nil, nil) retryBackend := New(be, 10, nil, nil)
var listed []string var listed []string
err := retryBackend.List(context.TODO(), restic.PackFile, func(fi restic.FileInfo) error { err := retryBackend.List(context.TODO(), restic.PackFile, func(fi restic.FileInfo) error {
@ -136,7 +136,7 @@ func TestBackendListRetryErrorFn(t *testing.T) {
} }
TestFastRetries(t) TestFastRetries(t)
retryBackend := NewRetryBackend(be, 10, nil, nil) retryBackend := New(be, 10, nil, nil)
var ErrTest = errors.New("test error") var ErrTest = errors.New("test error")
@ -193,7 +193,7 @@ func TestBackendListRetryErrorBackend(t *testing.T) {
TestFastRetries(t) TestFastRetries(t)
const maxRetries = 2 const maxRetries = 2
retryBackend := NewRetryBackend(be, maxRetries, nil, nil) retryBackend := New(be, maxRetries, nil, nil)
var listed []string var listed []string
err := retryBackend.List(context.TODO(), restic.PackFile, func(fi restic.FileInfo) error { err := retryBackend.List(context.TODO(), restic.PackFile, func(fi restic.FileInfo) error {
@ -263,7 +263,7 @@ func TestBackendLoadRetry(t *testing.T) {
} }
TestFastRetries(t) TestFastRetries(t)
retryBackend := NewRetryBackend(be, 10, nil, nil) retryBackend := New(be, 10, nil, nil)
var buf []byte var buf []byte
err := retryBackend.Load(context.TODO(), restic.Handle{}, 0, 0, func(rd io.Reader) (err error) { err := retryBackend.Load(context.TODO(), restic.Handle{}, 0, 0, func(rd io.Reader) (err error) {
@ -283,7 +283,7 @@ func TestBackendCanceledContext(t *testing.T) {
// unimplemented mock backend functions return an error by default // unimplemented mock backend functions return an error by default
// check that we received the expected context canceled error instead // check that we received the expected context canceled error instead
TestFastRetries(t) TestFastRetries(t)
retryBackend := NewRetryBackend(mock.NewBackend(), 2, nil, nil) retryBackend := New(mock.NewBackend(), 2, nil, nil)
h := restic.Handle{Type: restic.PackFile, Name: restic.NewRandomID().String()} h := restic.Handle{Type: restic.PackFile, Name: restic.NewRandomID().String()}
// create an already canceled context // create an already canceled context

View file

@ -1,4 +1,4 @@
package backend package retry
import "testing" import "testing"

View file

@ -13,6 +13,7 @@ import (
"time" "time"
"github.com/restic/restic/internal/backend" "github.com/restic/restic/internal/backend"
"github.com/restic/restic/internal/backend/layout"
"github.com/restic/restic/internal/backend/sema" "github.com/restic/restic/internal/backend/sema"
"github.com/restic/restic/internal/debug" "github.com/restic/restic/internal/debug"
"github.com/restic/restic/internal/errors" "github.com/restic/restic/internal/errors"
@ -28,7 +29,7 @@ type Backend struct {
client *minio.Client client *minio.Client
sem sema.Semaphore sem sema.Semaphore
cfg Config cfg Config
backend.Layout layout.Layout
} }
// make sure that *Backend implements backend.Backend // make sure that *Backend implements backend.Backend
@ -113,7 +114,7 @@ func open(ctx context.Context, cfg Config, rt http.RoundTripper) (*Backend, erro
cfg: cfg, cfg: cfg,
} }
l, err := backend.ParseLayout(ctx, be, cfg.Layout, defaultLayout, cfg.Prefix) l, err := layout.ParseLayout(ctx, be, cfg.Layout, defaultLayout, cfg.Prefix)
if err != nil { if err != nil {
return nil, err return nil, err
} }
@ -514,7 +515,7 @@ func (be *Backend) Delete(ctx context.Context) error {
func (be *Backend) Close() error { return nil } func (be *Backend) Close() error { return nil }
// Rename moves a file based on the new layout l. // Rename moves a file based on the new layout l.
func (be *Backend) Rename(ctx context.Context, h restic.Handle, l backend.Layout) error { func (be *Backend) Rename(ctx context.Context, h restic.Handle, l layout.Layout) error {
debug.Log("Rename %v to %v", h, l) debug.Log("Rename %v to %v", h, l)
oldname := be.Filename(h) oldname := be.Filename(h)
newname := l.Filename(h) newname := l.Filename(h)

View file

@ -14,6 +14,7 @@ import (
"time" "time"
"github.com/restic/restic/internal/backend" "github.com/restic/restic/internal/backend"
"github.com/restic/restic/internal/backend/layout"
"github.com/restic/restic/internal/backend/sema" "github.com/restic/restic/internal/backend/sema"
"github.com/restic/restic/internal/debug" "github.com/restic/restic/internal/debug"
"github.com/restic/restic/internal/errors" "github.com/restic/restic/internal/errors"
@ -35,7 +36,7 @@ type SFTP struct {
posixRename bool posixRename bool
sem sema.Semaphore sem sema.Semaphore
backend.Layout layout.Layout
Config Config
backend.Modes backend.Modes
} }
@ -144,14 +145,14 @@ func open(ctx context.Context, sftp *SFTP, cfg Config) (*SFTP, error) {
return nil, err return nil, err
} }
sftp.Layout, err = backend.ParseLayout(ctx, sftp, cfg.Layout, defaultLayout, cfg.Path) sftp.Layout, err = layout.ParseLayout(ctx, sftp, cfg.Layout, defaultLayout, cfg.Path)
if err != nil { if err != nil {
return nil, err return nil, err
} }
debug.Log("layout: %v\n", sftp.Layout) debug.Log("layout: %v\n", sftp.Layout)
fi, err := sftp.c.Stat(Join(cfg.Path, backend.Paths.Config)) fi, err := sftp.c.Stat(sftp.Layout.Filename(restic.Handle{Type: restic.ConfigFile}))
m := backend.DeriveModesFromFileInfo(fi, err) m := backend.DeriveModesFromFileInfo(fi, err)
debug.Log("using (%03O file, %03O dir) permissions", m.File, m.Dir) debug.Log("using (%03O file, %03O dir) permissions", m.File, m.Dir)
@ -243,7 +244,7 @@ func Create(ctx context.Context, cfg Config) (*SFTP, error) {
return nil, err return nil, err
} }
sftp.Layout, err = backend.ParseLayout(ctx, sftp, cfg.Layout, defaultLayout, cfg.Path) sftp.Layout, err = layout.ParseLayout(ctx, sftp, cfg.Layout, defaultLayout, cfg.Path)
if err != nil { if err != nil {
return nil, err return nil, err
} }
@ -251,7 +252,7 @@ func Create(ctx context.Context, cfg Config) (*SFTP, error) {
sftp.Modes = backend.DefaultModes sftp.Modes = backend.DefaultModes
// test if config file already exists // test if config file already exists
_, err = sftp.c.Lstat(Join(cfg.Path, backend.Paths.Config)) _, err = sftp.c.Lstat(sftp.Layout.Filename(restic.Handle{Type: restic.ConfigFile}))
if err == nil { if err == nil {
return nil, errors.New("config file already exists") return nil, errors.New("config file already exists")
} }

View file

@ -14,6 +14,7 @@ import (
"time" "time"
"github.com/restic/restic/internal/backend" "github.com/restic/restic/internal/backend"
"github.com/restic/restic/internal/backend/layout"
"github.com/restic/restic/internal/backend/sema" "github.com/restic/restic/internal/backend/sema"
"github.com/restic/restic/internal/debug" "github.com/restic/restic/internal/debug"
"github.com/restic/restic/internal/errors" "github.com/restic/restic/internal/errors"
@ -30,7 +31,7 @@ type beSwift struct {
sem sema.Semaphore sem sema.Semaphore
container string // Container name container string // Container name
prefix string // Prefix of object names in the container prefix string // Prefix of object names in the container
backend.Layout layout.Layout
} }
// ensure statically that *beSwift implements restic.Backend. // ensure statically that *beSwift implements restic.Backend.
@ -74,7 +75,7 @@ func Open(ctx context.Context, cfg Config, rt http.RoundTripper) (restic.Backend
sem: sem, sem: sem,
container: cfg.Container, container: cfg.Container,
prefix: cfg.Prefix, prefix: cfg.Prefix,
Layout: &backend.DefaultLayout{ Layout: &layout.DefaultLayout{
Path: cfg.Prefix, Path: cfg.Prefix,
Join: path.Join, Join: path.Join,
}, },

View file

@ -6,7 +6,7 @@ import (
"os" "os"
"path" "path"
"github.com/restic/restic/internal/backend" "github.com/restic/restic/internal/backend/layout"
"github.com/restic/restic/internal/backend/s3" "github.com/restic/restic/internal/backend/s3"
"github.com/restic/restic/internal/cache" "github.com/restic/restic/internal/cache"
"github.com/restic/restic/internal/debug" "github.com/restic/restic/internal/debug"
@ -74,7 +74,7 @@ func retry(max int, fail func(err error), f func() error) error {
// maxErrors for retrying renames on s3. // maxErrors for retrying renames on s3.
const maxErrors = 20 const maxErrors = 20
func (m *S3Layout) moveFiles(ctx context.Context, be *s3.Backend, l backend.Layout, t restic.FileType) error { func (m *S3Layout) moveFiles(ctx context.Context, be *s3.Backend, l layout.Layout, t restic.FileType) error {
printErr := func(err error) { printErr := func(err error) {
fmt.Fprintf(os.Stderr, "renaming file returned error: %v\n", err) fmt.Fprintf(os.Stderr, "renaming file returned error: %v\n", err)
} }
@ -97,12 +97,12 @@ func (m *S3Layout) Apply(ctx context.Context, repo restic.Repository) error {
return errors.New("backend is not s3") return errors.New("backend is not s3")
} }
oldLayout := &backend.S3LegacyLayout{ oldLayout := &layout.S3LegacyLayout{
Path: be.Path(), Path: be.Path(),
Join: path.Join, Join: path.Join,
} }
newLayout := &backend.DefaultLayout{ newLayout := &layout.DefaultLayout{
Path: be.Path(), Path: be.Path(),
Join: path.Join, Join: path.Join,
} }