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

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

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
}