From 6c6dceade37a2072e2c728136e0b3e4a6ea94202 Mon Sep 17 00:00:00 2001 From: Michael Eischer Date: Fri, 19 Apr 2024 22:26:14 +0200 Subject: [PATCH 1/2] global: unify backend open and create --- cmd/restic/global.go | 52 +++++++++++++++++--------------------------- 1 file changed, 20 insertions(+), 32 deletions(-) diff --git a/cmd/restic/global.go b/cmd/restic/global.go index cc47496f36c..c93fb4bce29 100644 --- a/cmd/restic/global.go +++ b/cmd/restic/global.go @@ -570,16 +570,13 @@ func parseConfig(loc location.Location, opts options.Options) (interface{}, erro return cfg, nil } -// Open the backend specified by a location config. -func open(ctx context.Context, s string, gopts GlobalOptions, opts options.Options) (backend.Backend, error) { +func innerOpen(ctx context.Context, s string, gopts GlobalOptions, opts options.Options, create bool) (backend.Backend, error) { debug.Log("parsing location %v", location.StripPassword(gopts.backends, s)) loc, err := location.Parse(gopts.backends, s) if err != nil { return nil, errors.Fatalf("parsing repository location failed: %v", err) } - var be backend.Backend - cfg, err := parseConfig(loc, opts) if err != nil { return nil, err @@ -599,7 +596,13 @@ func open(ctx context.Context, s string, gopts GlobalOptions, opts options.Optio return nil, errors.Fatalf("invalid backend: %q", loc.Scheme) } - be, err = factory.Open(ctx, cfg, rt, lim) + var be backend.Backend + if create { + be, err = factory.Create(ctx, cfg, rt, nil) + } else { + be, err = factory.Open(ctx, cfg, rt, lim) + } + if err != nil { return nil, errors.Fatalf("unable to open repository at %v: %v", location.StripPassword(gopts.backends, s), err) } @@ -615,6 +618,17 @@ func open(ctx context.Context, s string, gopts GlobalOptions, opts options.Optio } } + return be, nil +} + +// Open the backend specified by a location config. +func open(ctx context.Context, s string, gopts GlobalOptions, opts options.Options) (backend.Backend, error) { + + be, err := innerOpen(ctx, s, gopts, opts, false) + if err != nil { + return nil, err + } + // check if config is there fi, err := be.Stat(ctx, backend.Handle{Type: restic.ConfigFile}) if err != nil { @@ -630,31 +644,5 @@ func open(ctx context.Context, s string, gopts GlobalOptions, opts options.Optio // Create the backend specified by URI. func create(ctx context.Context, s string, gopts GlobalOptions, opts options.Options) (backend.Backend, error) { - debug.Log("parsing location %v", location.StripPassword(gopts.backends, s)) - loc, err := location.Parse(gopts.backends, s) - if err != nil { - return nil, err - } - - cfg, err := parseConfig(loc, opts) - if err != nil { - return nil, err - } - - rt, err := backend.Transport(globalOptions.TransportOptions) - if err != nil { - return nil, errors.Fatal(err.Error()) - } - - factory := gopts.backends.Lookup(loc.Scheme) - if factory == nil { - return nil, errors.Fatalf("invalid backend: %q", loc.Scheme) - } - - be, err := factory.Create(ctx, cfg, rt, nil) - if err != nil { - return nil, err - } - - return logger.New(sema.NewBackend(be)), nil + return innerOpen(ctx, s, gopts, opts, true) } From 5f263752d7f41bbec8e889e3e9bb27c5a20ad2ff Mon Sep 17 00:00:00 2001 From: Michael Eischer Date: Wed, 24 Apr 2024 20:42:30 +0200 Subject: [PATCH 2/2] init: also apply limiter for non-HTTP backend --- cmd/restic/global.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cmd/restic/global.go b/cmd/restic/global.go index c93fb4bce29..eded479ada6 100644 --- a/cmd/restic/global.go +++ b/cmd/restic/global.go @@ -598,7 +598,7 @@ func innerOpen(ctx context.Context, s string, gopts GlobalOptions, opts options. var be backend.Backend if create { - be, err = factory.Create(ctx, cfg, rt, nil) + be, err = factory.Create(ctx, cfg, rt, lim) } else { be, err = factory.Open(ctx, cfg, rt, lim) }