From c458e114d4d073f9a1f99bf24e54e5c67416fef1 Mon Sep 17 00:00:00 2001 From: Michael Eischer Date: Fri, 10 Apr 2020 11:31:32 +0200 Subject: [PATCH] pass context to Find / FindSnapshot This allows proper interruption of restic while it searches for snapshots or key files. --- cmd/restic/cmd_backup.go | 2 +- cmd/restic/cmd_cat.go | 2 +- cmd/restic/cmd_diff.go | 2 +- cmd/restic/cmd_dump.go | 2 +- cmd/restic/cmd_key.go | 2 +- cmd/restic/cmd_restore.go | 2 +- cmd/restic/find.go | 2 +- internal/checker/checker_test.go | 2 +- internal/repository/key.go | 2 +- internal/repository/repository.go | 4 ++-- internal/restic/backend_find.go | 8 ++++---- internal/restic/backend_find_test.go | 14 +++++++------- internal/restic/snapshot_find.go | 4 ++-- 13 files changed, 24 insertions(+), 24 deletions(-) diff --git a/cmd/restic/cmd_backup.go b/cmd/restic/cmd_backup.go index e42ba81d9..fd138888c 100644 --- a/cmd/restic/cmd_backup.go +++ b/cmd/restic/cmd_backup.go @@ -394,7 +394,7 @@ func collectTargets(opts BackupOptions, args []string) (targets []string, err er func findParentSnapshot(ctx context.Context, repo restic.Repository, opts BackupOptions, targets []string) (parentID *restic.ID, err error) { // Force using a parent if !opts.Force && opts.Parent != "" { - id, err := restic.FindSnapshot(repo, opts.Parent) + id, err := restic.FindSnapshot(ctx, repo, opts.Parent) if err != nil { return nil, errors.Fatalf("invalid id %q: %v", opts.Parent, err) } diff --git a/cmd/restic/cmd_cat.go b/cmd/restic/cmd_cat.go index 935a03dbc..281ed875f 100644 --- a/cmd/restic/cmd_cat.go +++ b/cmd/restic/cmd_cat.go @@ -59,7 +59,7 @@ func runCat(gopts GlobalOptions, args []string) error { } // find snapshot id with prefix - id, err = restic.FindSnapshot(repo, args[1]) + id, err = restic.FindSnapshot(gopts.ctx, repo, args[1]) if err != nil { return errors.Fatalf("could not find snapshot: %v\n", err) } diff --git a/cmd/restic/cmd_diff.go b/cmd/restic/cmd_diff.go index 1fc8b0458..a4873319f 100644 --- a/cmd/restic/cmd_diff.go +++ b/cmd/restic/cmd_diff.go @@ -53,7 +53,7 @@ func init() { } func loadSnapshot(ctx context.Context, repo *repository.Repository, desc string) (*restic.Snapshot, error) { - id, err := restic.FindSnapshot(repo, desc) + id, err := restic.FindSnapshot(ctx, repo, desc) if err != nil { return nil, err } diff --git a/cmd/restic/cmd_dump.go b/cmd/restic/cmd_dump.go index 4a9e57a37..b33863c4f 100644 --- a/cmd/restic/cmd_dump.go +++ b/cmd/restic/cmd_dump.go @@ -160,7 +160,7 @@ func runDump(opts DumpOptions, gopts GlobalOptions, args []string) error { Exitf(1, "latest snapshot for criteria not found: %v Paths:%v Hosts:%v", err, opts.Paths, opts.Hosts) } } else { - id, err = restic.FindSnapshot(repo, snapshotIDString) + id, err = restic.FindSnapshot(ctx, repo, snapshotIDString) if err != nil { Exitf(1, "invalid id %q: %v", snapshotIDString, err) } diff --git a/cmd/restic/cmd_key.go b/cmd/restic/cmd_key.go index d14ba400e..8a54d83e8 100644 --- a/cmd/restic/cmd_key.go +++ b/cmd/restic/cmd_key.go @@ -210,7 +210,7 @@ func runKey(gopts GlobalOptions, args []string) error { return err } - id, err := restic.Find(repo.Backend(), restic.KeyFile, args[1]) + id, err := restic.Find(ctx, repo.Backend(), restic.KeyFile, args[1]) if err != nil { return err } diff --git a/cmd/restic/cmd_restore.go b/cmd/restic/cmd_restore.go index e87bafbba..ec7d4a9e9 100644 --- a/cmd/restic/cmd_restore.go +++ b/cmd/restic/cmd_restore.go @@ -122,7 +122,7 @@ func runRestore(opts RestoreOptions, gopts GlobalOptions, args []string) error { Exitf(1, "latest snapshot for criteria not found: %v Paths:%v Hosts:%v", err, opts.Paths, opts.Hosts) } } else { - id, err = restic.FindSnapshot(repo, snapshotIDString) + id, err = restic.FindSnapshot(ctx, repo, snapshotIDString) if err != nil { Exitf(1, "invalid id %q: %v", snapshotIDString, err) } diff --git a/cmd/restic/find.go b/cmd/restic/find.go index 8d39e177f..70710e2cb 100644 --- a/cmd/restic/find.go +++ b/cmd/restic/find.go @@ -29,7 +29,7 @@ func FindFilteredSnapshots(ctx context.Context, repo *repository.Repository, hos continue } } else { - id, err = restic.FindSnapshot(repo, s) + id, err = restic.FindSnapshot(ctx, repo, s) if err != nil { Warnf("Ignoring %q, it is not a snapshot id\n", s) continue diff --git a/internal/checker/checker_test.go b/internal/checker/checker_test.go index 96047569b..ce12420eb 100644 --- a/internal/checker/checker_test.go +++ b/internal/checker/checker_test.go @@ -574,7 +574,7 @@ func benchmarkSnapshotScaling(t *testing.B, newSnapshots int) { chkr, repo, cleanup := loadBenchRepository(t) defer cleanup() - snID, err := restic.FindSnapshot(repo, "51d249d2") + snID, err := restic.FindSnapshot(context.TODO(), repo, "51d249d2") if err != nil { t.Fatal(err) } diff --git a/internal/repository/key.go b/internal/repository/key.go index 69eb7e04b..42045389f 100644 --- a/internal/repository/key.go +++ b/internal/repository/key.go @@ -116,7 +116,7 @@ func SearchKey(ctx context.Context, s *Repository, password string, maxKeys int, checked := 0 if len(keyHint) > 0 { - id, err := restic.Find(s.Backend(), restic.KeyFile, keyHint) + id, err := restic.Find(ctx, s.Backend(), restic.KeyFile, keyHint) if err == nil { key, err := OpenKey(ctx, s, id, password) diff --git a/internal/repository/repository.go b/internal/repository/repository.go index 3c6d9665f..1b4a41c31 100644 --- a/internal/repository/repository.go +++ b/internal/repository/repository.go @@ -72,8 +72,8 @@ func (r *Repository) UseCache(c *cache.Cache) { // PrefixLength returns the number of bytes required so that all prefixes of // all IDs of type t are unique. -func (r *Repository) PrefixLength(t restic.FileType) (int, error) { - return restic.PrefixLength(r.be, t) +func (r *Repository) PrefixLength(ctx context.Context, t restic.FileType) (int, error) { + return restic.PrefixLength(ctx, r.be, t) } // LoadAndDecrypt loads and decrypts the file with the given type and ID, using diff --git a/internal/restic/backend_find.go b/internal/restic/backend_find.go index 36b2ba4ca..ec85a957a 100644 --- a/internal/restic/backend_find.go +++ b/internal/restic/backend_find.go @@ -17,10 +17,10 @@ var ErrMultipleIDMatches = errors.New("multiple IDs with prefix found") // Find loads the list of all files of type t and searches for names which // start with prefix. If none is found, nil and ErrNoIDPrefixFound is returned. // If more than one is found, nil and ErrMultipleIDMatches is returned. -func Find(be Lister, t FileType, prefix string) (string, error) { +func Find(ctx context.Context, be Lister, t FileType, prefix string) (string, error) { match := "" - ctx, cancel := context.WithCancel(context.TODO()) + ctx, cancel := context.WithCancel(ctx) defer cancel() err := be.List(ctx, t, func(fi FileInfo) error { @@ -50,11 +50,11 @@ const minPrefixLength = 8 // PrefixLength returns the number of bytes required so that all prefixes of // all names of type t are unique. -func PrefixLength(be Lister, t FileType) (int, error) { +func PrefixLength(ctx context.Context, be Lister, t FileType) (int, error) { // load all IDs of the given type list := make([]string, 0, 100) - ctx, cancel := context.WithCancel(context.TODO()) + ctx, cancel := context.WithCancel(ctx) defer cancel() err := be.List(ctx, t, func(fi FileInfo) error { diff --git a/internal/restic/backend_find_test.go b/internal/restic/backend_find_test.go index d122b4f94..557e1367b 100644 --- a/internal/restic/backend_find_test.go +++ b/internal/restic/backend_find_test.go @@ -38,7 +38,7 @@ func TestFind(t *testing.T) { return nil } - f, err := Find(m, SnapshotFile, "20bdc1402a6fc9b633aa") + f, err := Find(context.TODO(), m, SnapshotFile, "20bdc1402a6fc9b633aa") if err != nil { t.Error(err) } @@ -47,7 +47,7 @@ func TestFind(t *testing.T) { t.Errorf("Wrong match returned want %s, got %s", expectedMatch, f) } - f, err = Find(m, SnapshotFile, "NotAPrefix") + f, err = Find(context.TODO(), m, SnapshotFile, "NotAPrefix") if err != ErrNoIDPrefixFound { t.Error("Expected no snapshots to be found.") } @@ -57,7 +57,7 @@ func TestFind(t *testing.T) { // Try to match with a prefix longer than any ID. extraLengthID := samples[0].String() + "f" - f, err = Find(m, SnapshotFile, extraLengthID) + f, err = Find(context.TODO(), m, SnapshotFile, extraLengthID) if err != ErrNoIDPrefixFound { t.Error("Expected no snapshots to be matched.") } @@ -66,7 +66,7 @@ func TestFind(t *testing.T) { } // Use a prefix that will match the prefix of multiple Ids in `samples`. - f, err = Find(m, SnapshotFile, "20bdc140") + f, err = Find(context.TODO(), m, SnapshotFile, "20bdc140") if err != ErrMultipleIDMatches { t.Error("Expected multiple snapshots to be matched.") } @@ -89,7 +89,7 @@ func TestPrefixLength(t *testing.T) { return nil } - l, err := PrefixLength(m, SnapshotFile) + l, err := PrefixLength(context.TODO(), m, SnapshotFile) if err != nil { t.Error(err) } @@ -98,7 +98,7 @@ func TestPrefixLength(t *testing.T) { } list = samples[:3] - l, err = PrefixLength(m, SnapshotFile) + l, err = PrefixLength(context.TODO(), m, SnapshotFile) if err != nil { t.Error(err) } @@ -107,7 +107,7 @@ func TestPrefixLength(t *testing.T) { } list = samples[3:] - l, err = PrefixLength(m, SnapshotFile) + l, err = PrefixLength(context.TODO(), m, SnapshotFile) if err != nil { t.Error(err) } diff --git a/internal/restic/snapshot_find.go b/internal/restic/snapshot_find.go index 184fcd38f..50395c814 100644 --- a/internal/restic/snapshot_find.go +++ b/internal/restic/snapshot_find.go @@ -74,10 +74,10 @@ func FindLatestSnapshot(ctx context.Context, repo Repository, targets []string, // FindSnapshot takes a string and tries to find a snapshot whose ID matches // the string as closely as possible. -func FindSnapshot(repo Repository, s string) (ID, error) { +func FindSnapshot(ctx context.Context, repo Repository, s string) (ID, error) { // find snapshot id with prefix - name, err := Find(repo.Backend(), SnapshotFile, s) + name, err := Find(ctx, repo.Backend(), SnapshotFile, s) if err != nil { return ID{}, err }