pass context to Find / FindSnapshot

This allows proper interruption of restic while it searches for
snapshots or key files.
This commit is contained in:
Michael Eischer
2020-04-10 11:31:32 +02:00
parent 5fd3dbccb7
commit c458e114d4
13 changed files with 24 additions and 24 deletions

View File

@@ -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)
}

View File

@@ -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)

View File

@@ -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

View File

@@ -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 {

View File

@@ -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)
}

View File

@@ -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
}