2
2
mirror of https://github.com/octoleo/restic.git synced 2024-12-01 17:23:57 +00:00

backend: pass context into every backend constructor

This commit is contained in:
Michael Eischer 2023-06-08 13:11:34 +02:00
parent 4df77e9f26
commit 56836364a4
9 changed files with 19 additions and 19 deletions

View File

@ -584,7 +584,7 @@ func open(ctx context.Context, s string, gopts GlobalOptions, opts options.Optio
case "s3": case "s3":
be, err = s3.Open(ctx, *cfg.(*s3.Config), rt) be, err = s3.Open(ctx, *cfg.(*s3.Config), rt)
case "gs": case "gs":
be, err = gs.Open(*cfg.(*gs.Config), rt) be, err = gs.Open(ctx, *cfg.(*gs.Config), rt)
case "azure": case "azure":
be, err = azure.Open(ctx, *cfg.(*azure.Config), rt) be, err = azure.Open(ctx, *cfg.(*azure.Config), rt)
case "swift": case "swift":
@ -592,9 +592,9 @@ func open(ctx context.Context, s string, gopts GlobalOptions, opts options.Optio
case "b2": case "b2":
be, err = b2.Open(ctx, *cfg.(*b2.Config), rt) be, err = b2.Open(ctx, *cfg.(*b2.Config), rt)
case "rest": case "rest":
be, err = rest.Open(*cfg.(*rest.Config), rt) be, err = rest.Open(ctx, *cfg.(*rest.Config), rt)
case "rclone": case "rclone":
be, err = rclone.Open(*cfg.(*rclone.Config), lim) be, err = rclone.Open(ctx, *cfg.(*rclone.Config), lim)
default: default:
return nil, errors.Fatalf("invalid backend: %q", loc.Scheme) return nil, errors.Fatalf("invalid backend: %q", loc.Scheme)

View File

@ -117,7 +117,7 @@ func open(cfg Config, rt http.RoundTripper) (*Backend, error) {
} }
// Open opens the gs backend at the specified bucket. // Open opens the gs backend at the specified bucket.
func Open(cfg Config, rt http.RoundTripper) (restic.Backend, error) { func Open(_ context.Context, cfg Config, rt http.RoundTripper) (restic.Backend, error) {
return open(cfg, rt) return open(cfg, rt)
} }

View File

@ -58,12 +58,12 @@ func newGSTestSuite(t testing.TB) *test.Suite[gs.Config] {
// OpenFn is a function that opens a previously created temporary repository. // OpenFn is a function that opens a previously created temporary repository.
Open: func(cfg gs.Config) (restic.Backend, error) { Open: func(cfg gs.Config) (restic.Backend, error) {
return gs.Open(cfg, tr) return gs.Open(context.TODO(), cfg, tr)
}, },
// CleanupFn removes data created during the tests. // CleanupFn removes data created during the tests.
Cleanup: func(cfg gs.Config) error { Cleanup: func(cfg gs.Config) error {
be, err := gs.Open(cfg, tr) be, err := gs.Open(context.TODO(), cfg, tr)
if err != nil { if err != nil {
return err return err
} }

View File

@ -134,7 +134,7 @@ func wrapConn(c *StdioConn, lim limiter.Limiter) *wrappedConn {
} }
// New initializes a Backend and starts the process. // New initializes a Backend and starts the process.
func newBackend(cfg Config, lim limiter.Limiter) (*Backend, error) { func newBackend(ctx context.Context, cfg Config, lim limiter.Limiter) (*Backend, error) {
var ( var (
args []string args []string
err error err error
@ -197,7 +197,7 @@ func newBackend(cfg Config, lim limiter.Limiter) (*Backend, error) {
wg: wg, wg: wg,
} }
ctx, cancel := context.WithCancel(context.Background()) ctx, cancel := context.WithCancel(ctx)
defer cancel() defer cancel()
wg.Add(1) wg.Add(1)
@ -256,8 +256,8 @@ func newBackend(cfg Config, lim limiter.Limiter) (*Backend, error) {
} }
// Open starts an rclone process with the given config. // Open starts an rclone process with the given config.
func Open(cfg Config, lim limiter.Limiter) (*Backend, error) { func Open(ctx context.Context, cfg Config, lim limiter.Limiter) (*Backend, error) {
be, err := newBackend(cfg, lim) be, err := newBackend(ctx, cfg, lim)
if err != nil { if err != nil {
return nil, err return nil, err
} }
@ -272,7 +272,7 @@ func Open(cfg Config, lim limiter.Limiter) (*Backend, error) {
URL: url, URL: url,
} }
restBackend, err := rest.Open(restConfig, debug.RoundTripper(be.tr)) restBackend, err := rest.Open(ctx, restConfig, debug.RoundTripper(be.tr))
if err != nil { if err != nil {
_ = be.Close() _ = be.Close()
return nil, err return nil, err
@ -284,7 +284,7 @@ func Open(cfg Config, lim limiter.Limiter) (*Backend, error) {
// Create initializes a new restic repo with rclone. // Create initializes a new restic repo with rclone.
func Create(ctx context.Context, cfg Config) (*Backend, error) { func Create(ctx context.Context, cfg Config) (*Backend, error) {
be, err := newBackend(cfg, nil) be, err := newBackend(ctx, cfg, nil)
if err != nil { if err != nil {
return nil, err return nil, err
} }

View File

@ -39,7 +39,7 @@ func newTestSuite(t testing.TB) *test.Suite[rclone.Config] {
// OpenFn is a function that opens a previously created temporary repository. // OpenFn is a function that opens a previously created temporary repository.
Open: func(cfg rclone.Config) (restic.Backend, error) { Open: func(cfg rclone.Config) (restic.Backend, error) {
t.Logf("Open()") t.Logf("Open()")
return rclone.Open(cfg, nil) return rclone.Open(context.TODO(), cfg, nil)
}, },
} }
} }

View File

@ -15,7 +15,7 @@ func TestRcloneExit(t *testing.T) {
dir := rtest.TempDir(t) dir := rtest.TempDir(t)
cfg := NewConfig() cfg := NewConfig()
cfg.Remote = dir cfg.Remote = dir
be, err := Open(cfg, nil) be, err := Open(context.TODO(), cfg, nil)
var e *exec.Error var e *exec.Error
if errors.As(err, &e) && e.Err == exec.ErrNotFound { if errors.As(err, &e) && e.Err == exec.ErrNotFound {
t.Skipf("program %q not found", e.Name) t.Skipf("program %q not found", e.Name)
@ -45,7 +45,7 @@ func TestRcloneFailedStart(t *testing.T) {
cfg := NewConfig() cfg := NewConfig()
// exits with exit code 1 // exits with exit code 1
cfg.Program = "false" cfg.Program = "false"
_, err := Open(cfg, nil) _, err := Open(context.TODO(), cfg, nil)
var e *exec.ExitError var e *exec.ExitError
if !errors.As(err, &e) { if !errors.As(err, &e) {
// unexpected error // unexpected error

View File

@ -36,7 +36,7 @@ const (
) )
// Open opens the REST backend with the given config. // Open opens the REST backend with the given config.
func Open(cfg Config, rt http.RoundTripper) (*Backend, error) { func Open(_ context.Context, cfg Config, rt http.RoundTripper) (*Backend, error) {
// use url without trailing slash for layout // use url without trailing slash for layout
url := cfg.URL.String() url := cfg.URL.String()
if url[len(url)-1] == '/' { if url[len(url)-1] == '/' {
@ -55,7 +55,7 @@ func Open(cfg Config, rt http.RoundTripper) (*Backend, error) {
// Create creates a new REST on server configured in config. // Create creates a new REST on server configured in config.
func Create(ctx context.Context, cfg Config, rt http.RoundTripper) (*Backend, error) { func Create(ctx context.Context, cfg Config, rt http.RoundTripper) (*Backend, error) {
be, err := Open(cfg, rt) be, err := Open(ctx, cfg, rt)
if err != nil { if err != nil {
return nil, err return nil, err
} }

View File

@ -117,7 +117,7 @@ func TestListAPI(t *testing.T) {
URL: srvURL, URL: srvURL,
} }
be, err := rest.Open(cfg, http.DefaultTransport) be, err := rest.Open(context.TODO(), cfg, http.DefaultTransport)
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
} }

View File

@ -90,7 +90,7 @@ func newTestSuite(_ context.Context, t testing.TB, url *url.URL, minimalData boo
// OpenFn is a function that opens a previously created temporary repository. // OpenFn is a function that opens a previously created temporary repository.
Open: func(cfg rest.Config) (restic.Backend, error) { Open: func(cfg rest.Config) (restic.Backend, error) {
return rest.Open(cfg, tr) return rest.Open(context.TODO(), cfg, tr)
}, },
// CleanupFn removes data created during the tests. // CleanupFn removes data created during the tests.