2
2
mirror of https://github.com/octoleo/restic.git synced 2024-11-22 21:05:10 +00:00

Merge pull request #2933 from MichaelEischer/less-context-todo

Replace most usages of context.TODO()
This commit is contained in:
Alexander Neumann 2020-10-10 15:03:44 +02:00 committed by GitHub
commit f0d49ca600
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
42 changed files with 118 additions and 111 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) { func findParentSnapshot(ctx context.Context, repo restic.Repository, opts BackupOptions, targets []string) (parentID *restic.ID, err error) {
// Force using a parent // Force using a parent
if !opts.Force && opts.Parent != "" { if !opts.Force && opts.Parent != "" {
id, err := restic.FindSnapshot(repo, opts.Parent) id, err := restic.FindSnapshot(ctx, repo, opts.Parent)
if err != nil { if err != nil {
return nil, errors.Fatalf("invalid id %q: %v", opts.Parent, err) return nil, errors.Fatalf("invalid id %q: %v", opts.Parent, err)
} }
@ -496,7 +496,7 @@ func runBackup(opts BackupOptions, gopts GlobalOptions, term *termstatus.Termina
if !gopts.JSON { if !gopts.JSON {
p.V("lock repository") p.V("lock repository")
} }
lock, err := lockRepo(repo) lock, err := lockRepo(gopts.ctx, repo)
defer unlockRepo(lock) defer unlockRepo(lock)
if err != nil { if err != nil {
return err return err

View File

@ -42,7 +42,7 @@ func runCat(gopts GlobalOptions, args []string) error {
return err return err
} }
lock, err := lockRepo(repo) lock, err := lockRepo(gopts.ctx, repo)
defer unlockRepo(lock) defer unlockRepo(lock)
if err != nil { if err != nil {
return err return err
@ -59,7 +59,7 @@ func runCat(gopts GlobalOptions, args []string) error {
} }
// find snapshot id with prefix // find snapshot id with prefix
id, err = restic.FindSnapshot(repo, args[1]) id, err = restic.FindSnapshot(gopts.ctx, repo, args[1])
if err != nil { if err != nil {
return errors.Fatalf("could not find snapshot: %v\n", err) return errors.Fatalf("could not find snapshot: %v\n", err)
} }

View File

@ -158,7 +158,7 @@ func runCheck(opts CheckOptions, gopts GlobalOptions, args []string) error {
if !gopts.NoLock { if !gopts.NoLock {
Verbosef("create exclusive lock for repository\n") Verbosef("create exclusive lock for repository\n")
lock, err := lockRepoExclusive(repo) lock, err := lockRepoExclusive(gopts.ctx, repo)
defer unlockRepo(lock) defer unlockRepo(lock)
if err != nil { if err != nil {
return err return err

View File

@ -65,13 +65,13 @@ func runCopy(opts CopyOptions, gopts GlobalOptions, args []string) error {
return err return err
} }
srcLock, err := lockRepo(srcRepo) srcLock, err := lockRepo(ctx, srcRepo)
defer unlockRepo(srcLock) defer unlockRepo(srcLock)
if err != nil { if err != nil {
return err return err
} }
dstLock, err := lockRepo(dstRepo) dstLock, err := lockRepo(ctx, dstRepo)
defer unlockRepo(dstLock) defer unlockRepo(dstLock)
if err != nil { if err != nil {
return err return err

View File

@ -54,9 +54,9 @@ func prettyPrintJSON(wr io.Writer, item interface{}) error {
return err return err
} }
func debugPrintSnapshots(repo *repository.Repository, wr io.Writer) error { func debugPrintSnapshots(ctx context.Context, repo *repository.Repository, wr io.Writer) error {
return repo.List(context.TODO(), restic.SnapshotFile, func(id restic.ID, size int64) error { return repo.List(ctx, restic.SnapshotFile, func(id restic.ID, size int64) error {
snapshot, err := restic.LoadSnapshot(context.TODO(), repo, id) snapshot, err := restic.LoadSnapshot(ctx, repo, id)
if err != nil { if err != nil {
return err return err
} }
@ -82,12 +82,12 @@ type Blob struct {
Offset uint `json:"offset"` Offset uint `json:"offset"`
} }
func printPacks(repo *repository.Repository, wr io.Writer) error { func printPacks(ctx context.Context, repo *repository.Repository, wr io.Writer) error {
return repo.List(context.TODO(), restic.PackFile, func(id restic.ID, size int64) error { return repo.List(ctx, restic.PackFile, func(id restic.ID, size int64) error {
h := restic.Handle{Type: restic.PackFile, Name: id.String()} h := restic.Handle{Type: restic.PackFile, Name: id.String()}
blobs, err := pack.List(repo.Key(), restic.ReaderAt(repo.Backend(), h), size) blobs, err := pack.List(repo.Key(), restic.ReaderAt(ctx, repo.Backend(), h), size)
if err != nil { if err != nil {
Warnf("error for pack %v: %v\n", id.Str(), err) Warnf("error for pack %v: %v\n", id.Str(), err)
return nil return nil
@ -110,11 +110,11 @@ func printPacks(repo *repository.Repository, wr io.Writer) error {
}) })
} }
func dumpIndexes(repo restic.Repository, wr io.Writer) error { func dumpIndexes(ctx context.Context, repo restic.Repository, wr io.Writer) error {
return repo.List(context.TODO(), restic.IndexFile, func(id restic.ID, size int64) error { return repo.List(ctx, restic.IndexFile, func(id restic.ID, size int64) error {
Printf("index_id: %v\n", id) Printf("index_id: %v\n", id)
idx, err := repository.LoadIndex(context.TODO(), repo, id) idx, err := repository.LoadIndex(ctx, repo, id)
if err != nil { if err != nil {
return err return err
} }
@ -134,7 +134,7 @@ func runDebugDump(gopts GlobalOptions, args []string) error {
} }
if !gopts.NoLock { if !gopts.NoLock {
lock, err := lockRepo(repo) lock, err := lockRepo(gopts.ctx, repo)
defer unlockRepo(lock) defer unlockRepo(lock)
if err != nil { if err != nil {
return err return err
@ -145,20 +145,20 @@ func runDebugDump(gopts GlobalOptions, args []string) error {
switch tpe { switch tpe {
case "indexes": case "indexes":
return dumpIndexes(repo, gopts.stdout) return dumpIndexes(gopts.ctx, repo, gopts.stdout)
case "snapshots": case "snapshots":
return debugPrintSnapshots(repo, gopts.stdout) return debugPrintSnapshots(gopts.ctx, repo, gopts.stdout)
case "packs": case "packs":
return printPacks(repo, gopts.stdout) return printPacks(gopts.ctx, repo, gopts.stdout)
case "all": case "all":
Printf("snapshots:\n") Printf("snapshots:\n")
err := debugPrintSnapshots(repo, gopts.stdout) err := debugPrintSnapshots(gopts.ctx, repo, gopts.stdout)
if err != nil { if err != nil {
return err return err
} }
Printf("\nindexes:\n") Printf("\nindexes:\n")
err = dumpIndexes(repo, gopts.stdout) err = dumpIndexes(gopts.ctx, repo, gopts.stdout)
if err != nil { if err != nil {
return err return err
} }

View File

@ -53,7 +53,7 @@ func init() {
} }
func loadSnapshot(ctx context.Context, repo *repository.Repository, desc string) (*restic.Snapshot, error) { 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 { if err != nil {
return nil, err return nil, err
} }
@ -332,7 +332,7 @@ func runDiff(opts DiffOptions, gopts GlobalOptions, args []string) error {
} }
if !gopts.NoLock { if !gopts.NoLock {
lock, err := lockRepo(repo) lock, err := lockRepo(ctx, repo)
defer unlockRepo(lock) defer unlockRepo(lock)
if err != nil { if err != nil {
return err return err

View File

@ -140,7 +140,7 @@ func runDump(opts DumpOptions, gopts GlobalOptions, args []string) error {
} }
if !gopts.NoLock { if !gopts.NoLock {
lock, err := lockRepo(repo) lock, err := lockRepo(ctx, repo)
defer unlockRepo(lock) defer unlockRepo(lock)
if err != nil { if err != nil {
return err return err
@ -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) Exitf(1, "latest snapshot for criteria not found: %v Paths:%v Hosts:%v", err, opts.Paths, opts.Hosts)
} }
} else { } else {
id, err = restic.FindSnapshot(repo, snapshotIDString) id, err = restic.FindSnapshot(ctx, repo, snapshotIDString)
if err != nil { if err != nil {
Exitf(1, "invalid id %q: %v", snapshotIDString, err) Exitf(1, "invalid id %q: %v", snapshotIDString, err)
} }

View File

@ -529,7 +529,7 @@ func runFind(opts FindOptions, gopts GlobalOptions, args []string) error {
} }
if !gopts.NoLock { if !gopts.NoLock {
lock, err := lockRepo(repo) lock, err := lockRepo(gopts.ctx, repo)
defer unlockRepo(lock) defer unlockRepo(lock)
if err != nil { if err != nil {
return err return err

View File

@ -88,7 +88,7 @@ func runForget(opts ForgetOptions, gopts GlobalOptions, args []string) error {
return err return err
} }
lock, err := lockRepoExclusive(repo) lock, err := lockRepoExclusive(gopts.ctx, repo)
defer unlockRepo(lock) defer unlockRepo(lock)
if err != nil { if err != nil {
return err return err

View File

@ -188,7 +188,7 @@ func runKey(gopts GlobalOptions, args []string) error {
switch args[0] { switch args[0] {
case "list": case "list":
lock, err := lockRepo(repo) lock, err := lockRepo(ctx, repo)
defer unlockRepo(lock) defer unlockRepo(lock)
if err != nil { if err != nil {
return err return err
@ -196,7 +196,7 @@ func runKey(gopts GlobalOptions, args []string) error {
return listKeys(ctx, repo, gopts) return listKeys(ctx, repo, gopts)
case "add": case "add":
lock, err := lockRepo(repo) lock, err := lockRepo(ctx, repo)
defer unlockRepo(lock) defer unlockRepo(lock)
if err != nil { if err != nil {
return err return err
@ -204,20 +204,20 @@ func runKey(gopts GlobalOptions, args []string) error {
return addKey(gopts, repo) return addKey(gopts, repo)
case "remove": case "remove":
lock, err := lockRepoExclusive(repo) lock, err := lockRepoExclusive(ctx, repo)
defer unlockRepo(lock) defer unlockRepo(lock)
if err != nil { if err != nil {
return err 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 { if err != nil {
return err return err
} }
return deleteKey(gopts.ctx, repo, id) return deleteKey(gopts.ctx, repo, id)
case "passwd": case "passwd":
lock, err := lockRepoExclusive(repo) lock, err := lockRepoExclusive(ctx, repo)
defer unlockRepo(lock) defer unlockRepo(lock)
if err != nil { if err != nil {
return err return err

View File

@ -40,7 +40,7 @@ func runList(cmd *cobra.Command, opts GlobalOptions, args []string) error {
} }
if !opts.NoLock { if !opts.NoLock {
lock, err := lockRepo(repo) lock, err := lockRepo(opts.ctx, repo)
defer unlockRepo(lock) defer unlockRepo(lock)
if err != nil { if err != nil {
return err return err

View File

@ -99,7 +99,7 @@ func runMigrate(opts MigrateOptions, gopts GlobalOptions, args []string) error {
return err return err
} }
lock, err := lockRepoExclusive(repo) lock, err := lockRepoExclusive(gopts.ctx, repo)
defer unlockRepo(lock) defer unlockRepo(lock)
if err != nil { if err != nil {
return err return err

View File

@ -91,7 +91,7 @@ func mount(opts MountOptions, gopts GlobalOptions, mountpoint string) error {
} }
if !gopts.NoLock { if !gopts.NoLock {
lock, err := lockRepo(repo) lock, err := lockRepo(gopts.ctx, repo)
defer unlockRepo(lock) defer unlockRepo(lock)
if err != nil { if err != nil {
return err return err

View File

@ -50,7 +50,7 @@ func runPrune(gopts GlobalOptions) error {
return err return err
} }
lock, err := lockRepoExclusive(repo) lock, err := lockRepoExclusive(gopts.ctx, repo)
defer unlockRepo(lock) defer unlockRepo(lock)
if err != nil { if err != nil {
return err return err

View File

@ -38,7 +38,7 @@ func runRebuildIndex(gopts GlobalOptions) error {
return err return err
} }
lock, err := lockRepoExclusive(repo) lock, err := lockRepoExclusive(gopts.ctx, repo)
defer unlockRepo(lock) defer unlockRepo(lock)
if err != nil { if err != nil {
return err return err

View File

@ -43,7 +43,7 @@ func runRecover(gopts GlobalOptions) error {
return err return err
} }
lock, err := lockRepo(repo) lock, err := lockRepo(gopts.ctx, repo)
defer unlockRepo(lock) defer unlockRepo(lock)
if err != nil { if err != nil {
return err return err

View File

@ -102,7 +102,7 @@ func runRestore(opts RestoreOptions, gopts GlobalOptions, args []string) error {
} }
if !gopts.NoLock { if !gopts.NoLock {
lock, err := lockRepo(repo) lock, err := lockRepo(ctx, repo)
defer unlockRepo(lock) defer unlockRepo(lock)
if err != nil { if err != nil {
return err return err
@ -122,13 +122,13 @@ 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) Exitf(1, "latest snapshot for criteria not found: %v Paths:%v Hosts:%v", err, opts.Paths, opts.Hosts)
} }
} else { } else {
id, err = restic.FindSnapshot(repo, snapshotIDString) id, err = restic.FindSnapshot(ctx, repo, snapshotIDString)
if err != nil { if err != nil {
Exitf(1, "invalid id %q: %v", snapshotIDString, err) Exitf(1, "invalid id %q: %v", snapshotIDString, err)
} }
} }
res, err := restorer.NewRestorer(repo, id) res, err := restorer.NewRestorer(ctx, repo, id)
if err != nil { if err != nil {
Exitf(2, "creating restorer failed: %v\n", err) Exitf(2, "creating restorer failed: %v\n", err)
} }

View File

@ -61,7 +61,7 @@ func runSnapshots(opts SnapshotOptions, gopts GlobalOptions, args []string) erro
} }
if !gopts.NoLock { if !gopts.NoLock {
lock, err := lockRepo(repo) lock, err := lockRepo(gopts.ctx, repo)
defer unlockRepo(lock) defer unlockRepo(lock)
if err != nil { if err != nil {
return err return err

View File

@ -91,7 +91,7 @@ func runStats(gopts GlobalOptions, args []string) error {
} }
if !gopts.NoLock { if !gopts.NoLock {
lock, err := lockRepo(repo) lock, err := lockRepo(ctx, repo)
defer unlockRepo(lock) defer unlockRepo(lock)
if err != nil { if err != nil {
return err return err

View File

@ -119,7 +119,7 @@ func runTag(opts TagOptions, gopts GlobalOptions, args []string) error {
if !gopts.NoLock { if !gopts.NoLock {
Verbosef("create exclusive lock for repository\n") Verbosef("create exclusive lock for repository\n")
lock, err := lockRepoExclusive(repo) lock, err := lockRepoExclusive(gopts.ctx, repo)
defer unlockRepo(lock) defer unlockRepo(lock)
if err != nil { if err != nil {
return err return err

View File

@ -29,7 +29,7 @@ func FindFilteredSnapshots(ctx context.Context, repo *repository.Repository, hos
continue continue
} }
} else { } else {
id, err = restic.FindSnapshot(repo, s) id, err = restic.FindSnapshot(ctx, repo, s)
if err != nil { if err != nil {
Warnf("Ignoring %q, it is not a snapshot id\n", s) Warnf("Ignoring %q, it is not a snapshot id\n", s)
continue continue

View File

@ -768,9 +768,9 @@ func create(s string, opts options.Options) (restic.Backend, error) {
case "b2": case "b2":
return b2.Create(globalOptions.ctx, cfg.(b2.Config), rt) return b2.Create(globalOptions.ctx, cfg.(b2.Config), rt)
case "rest": case "rest":
return rest.Create(cfg.(rest.Config), rt) return rest.Create(globalOptions.ctx, cfg.(rest.Config), rt)
case "rclone": case "rclone":
return rclone.Create(cfg.(rclone.Config)) return rclone.Create(globalOptions.ctx, cfg.(rclone.Config))
} }
debug.Log("invalid repository scheme: %v", s) debug.Log("invalid repository scheme: %v", s)

View File

@ -6,7 +6,6 @@
package main package main
import ( import (
"context"
"fmt" "fmt"
"os" "os"
"path/filepath" "path/filepath"
@ -122,7 +121,7 @@ func checkSnapshots(t testing.TB, global GlobalOptions, repo *repository.Reposit
} }
for _, id := range snapshotIDs { for _, id := range snapshotIDs {
snapshot, err := restic.LoadSnapshot(context.TODO(), repo, id) snapshot, err := restic.LoadSnapshot(global.ctx, repo, id)
rtest.OK(t, err) rtest.OK(t, err)
ts := snapshot.Time.Format(time.RFC3339) ts := snapshot.Time.Format(time.RFC3339)

View File

@ -18,21 +18,21 @@ var globalLocks struct {
sync.Mutex sync.Mutex
} }
func lockRepo(repo *repository.Repository) (*restic.Lock, error) { func lockRepo(ctx context.Context, repo *repository.Repository) (*restic.Lock, error) {
return lockRepository(repo, false) return lockRepository(ctx, repo, false)
} }
func lockRepoExclusive(repo *repository.Repository) (*restic.Lock, error) { func lockRepoExclusive(ctx context.Context, repo *repository.Repository) (*restic.Lock, error) {
return lockRepository(repo, true) return lockRepository(ctx, repo, true)
} }
func lockRepository(repo *repository.Repository, exclusive bool) (*restic.Lock, error) { func lockRepository(ctx context.Context, repo *repository.Repository, exclusive bool) (*restic.Lock, error) {
lockFn := restic.NewLock lockFn := restic.NewLock
if exclusive { if exclusive {
lockFn = restic.NewExclusiveLock lockFn = restic.NewExclusiveLock
} }
lock, err := lockFn(context.TODO(), repo) lock, err := lockFn(ctx, repo)
if err != nil { if err != nil {
return nil, errors.WithMessage(err, "unable to create lock in backend") return nil, errors.WithMessage(err, "unable to create lock in backend")
} }
@ -86,6 +86,10 @@ func refreshLocks(wg *sync.WaitGroup, done <-chan struct{}) {
} }
func unlockRepo(lock *restic.Lock) error { func unlockRepo(lock *restic.Lock) error {
if lock == nil {
return nil
}
globalLocks.Lock() globalLocks.Lock()
defer globalLocks.Unlock() defer globalLocks.Unlock()

View File

@ -275,8 +275,8 @@ func Open(cfg Config, lim limiter.Limiter) (*Backend, error) {
return be, nil return be, nil
} }
// Create initializes a new restic repo with clone. // Create initializes a new restic repo with rclone.
func Create(cfg Config) (*Backend, error) { func Create(ctx context.Context, cfg Config) (*Backend, error) {
be, err := newBackend(cfg, nil) be, err := newBackend(cfg, nil)
if err != nil { if err != nil {
return nil, err return nil, err
@ -294,7 +294,7 @@ func Create(cfg Config) (*Backend, error) {
URL: url, URL: url,
} }
restBackend, err := rest.Create(restConfig, debug.RoundTripper(be.tr)) restBackend, err := rest.Create(ctx, restConfig, debug.RoundTripper(be.tr))
if err != nil { if err != nil {
_ = be.Close() _ = be.Close()
return nil, err return nil, err

View File

@ -1,6 +1,7 @@
package rclone_test package rclone_test
import ( import (
"context"
"os/exec" "os/exec"
"testing" "testing"
@ -27,7 +28,7 @@ func newTestSuite(t testing.TB) *test.Suite {
Create: func(config interface{}) (restic.Backend, error) { Create: func(config interface{}) (restic.Backend, error) {
t.Logf("Create()") t.Logf("Create()")
cfg := config.(rclone.Config) cfg := config.(rclone.Config)
be, err := rclone.Create(cfg) be, err := rclone.Create(context.TODO(), cfg)
if e, ok := errors.Cause(err).(*exec.Error); ok && e.Err == exec.ErrNotFound { if e, ok := errors.Cause(err).(*exec.Error); ok && e.Err == exec.ErrNotFound {
t.Skipf("program %q not found", e.Name) t.Skipf("program %q not found", e.Name)
return nil, nil return nil, nil

View File

@ -63,13 +63,13 @@ 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(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(cfg, rt)
if err != nil { if err != nil {
return nil, err return nil, err
} }
_, err = be.Stat(context.TODO(), restic.Handle{Type: restic.ConfigFile}) _, err = be.Stat(ctx, restic.Handle{Type: restic.ConfigFile})
if err == nil { if err == nil {
return nil, errors.Fatal("config file already exists") return nil, errors.Fatal("config file already exists")
} }

View File

@ -86,7 +86,7 @@ func newTestSuite(ctx context.Context, t testing.TB, url *url.URL, minimalData b
// CreateFn is a function that creates a temporary repository for the tests. // CreateFn is a function that creates a temporary repository for the tests.
Create: func(config interface{}) (restic.Backend, error) { Create: func(config interface{}) (restic.Backend, error) {
cfg := config.(rest.Config) cfg := config.(rest.Config)
return rest.Create(cfg, tr) return rest.Create(context.TODO(), cfg, tr)
}, },
// OpenFn is a function that opens a previously created temporary repository. // OpenFn is a function that opens a previously created temporary repository.

View File

@ -574,7 +574,7 @@ func benchmarkSnapshotScaling(t *testing.B, newSnapshots int) {
chkr, repo, cleanup := loadBenchRepository(t) chkr, repo, cleanup := loadBenchRepository(t)
defer cleanup() defer cleanup()
snID, err := restic.FindSnapshot(repo, "51d249d2") snID, err := restic.FindSnapshot(context.TODO(), repo, "51d249d2")
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
} }

View File

@ -128,7 +128,7 @@ func TestUnpackReadSeeker(t *testing.T) {
handle := restic.Handle{Type: restic.PackFile, Name: id.String()} handle := restic.Handle{Type: restic.PackFile, Name: id.String()}
rtest.OK(t, b.Save(context.TODO(), handle, restic.NewByteReader(packData))) rtest.OK(t, b.Save(context.TODO(), handle, restic.NewByteReader(packData)))
verifyBlobs(t, bufs, k, restic.ReaderAt(b, handle), packSize) verifyBlobs(t, bufs, k, restic.ReaderAt(context.TODO(), b, handle), packSize)
} }
func TestShortPack(t *testing.T) { func TestShortPack(t *testing.T) {
@ -141,5 +141,5 @@ func TestShortPack(t *testing.T) {
handle := restic.Handle{Type: restic.PackFile, Name: id.String()} handle := restic.Handle{Type: restic.PackFile, Name: id.String()}
rtest.OK(t, b.Save(context.TODO(), handle, restic.NewByteReader(packData))) rtest.OK(t, b.Save(context.TODO(), handle, restic.NewByteReader(packData)))
verifyBlobs(t, bufs, k, restic.ReaderAt(b, handle), packSize) verifyBlobs(t, bufs, k, restic.ReaderAt(context.TODO(), b, handle), packSize)
} }

View File

@ -57,8 +57,8 @@ var (
// createMasterKey creates a new master key in the given backend and encrypts // createMasterKey creates a new master key in the given backend and encrypts
// it with the password. // it with the password.
func createMasterKey(s *Repository, password string) (*Key, error) { func createMasterKey(ctx context.Context, s *Repository, password string) (*Key, error) {
return AddKey(context.TODO(), s, password, "", "", nil) return AddKey(ctx, s, password, "", "", nil)
} }
// OpenKey tries do decrypt the key specified by name with the given password. // OpenKey tries do decrypt the key specified by name with the given password.
@ -116,7 +116,7 @@ func SearchKey(ctx context.Context, s *Repository, password string, maxKeys int,
checked := 0 checked := 0
if len(keyHint) > 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 { if err == nil {
key, err := OpenKey(ctx, s, id, password) key, err := OpenKey(ctx, s, id, password)

View File

@ -267,7 +267,7 @@ func (mi *MasterIndex) MergeFinalIndexes() {
// RebuildIndex combines all known indexes to a new index, leaving out any // RebuildIndex combines all known indexes to a new index, leaving out any
// packs whose ID is contained in packBlacklist. The new index contains the IDs // packs whose ID is contained in packBlacklist. The new index contains the IDs
// of all known indexes in the "supersedes" field. // of all known indexes in the "supersedes" field.
func (mi *MasterIndex) RebuildIndex(packBlacklist restic.IDSet) (*Index, error) { func (mi *MasterIndex) RebuildIndex(ctx context.Context, packBlacklist restic.IDSet) (*Index, error) {
mi.idxMutex.Lock() mi.idxMutex.Lock()
defer mi.idxMutex.Unlock() defer mi.idxMutex.Unlock()
@ -275,7 +275,7 @@ func (mi *MasterIndex) RebuildIndex(packBlacklist restic.IDSet) (*Index, error)
newIndex := NewIndex() newIndex := NewIndex()
ctx, cancel := context.WithCancel(context.TODO()) ctx, cancel := context.WithCancel(ctx)
defer cancel() defer cancel()
for i, idx := range mi.idx { for i, idx := range mi.idx {

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 // PrefixLength returns the number of bytes required so that all prefixes of
// all IDs of type t are unique. // all IDs of type t are unique.
func (r *Repository) PrefixLength(t restic.FileType) (int, error) { func (r *Repository) PrefixLength(ctx context.Context, t restic.FileType) (int, error) {
return restic.PrefixLength(r.be, t) return restic.PrefixLength(ctx, r.be, t)
} }
// LoadAndDecrypt loads and decrypts the file with the given type and ID, using // LoadAndDecrypt loads and decrypts the file with the given type and ID, using
@ -638,7 +638,7 @@ func (r *Repository) Init(ctx context.Context, password string, chunkerPolynomia
// init creates a new master key with the supplied password and uses it to save // init creates a new master key with the supplied password and uses it to save
// the config into the repo. // the config into the repo.
func (r *Repository) init(ctx context.Context, password string, cfg restic.Config) error { func (r *Repository) init(ctx context.Context, password string, cfg restic.Config) error {
key, err := createMasterKey(r, password) key, err := createMasterKey(ctx, r, password)
if err != nil { if err != nil {
return err return err
} }
@ -679,7 +679,7 @@ func (r *Repository) List(ctx context.Context, t restic.FileType, fn func(restic
func (r *Repository) ListPack(ctx context.Context, id restic.ID, size int64) ([]restic.Blob, int64, error) { func (r *Repository) ListPack(ctx context.Context, id restic.ID, size int64) ([]restic.Blob, int64, error) {
h := restic.Handle{Type: restic.PackFile, Name: id.String()} h := restic.Handle{Type: restic.PackFile, Name: id.String()}
blobs, err := pack.List(r.Key(), restic.ReaderAt(r.Backend(), h), size) blobs, err := pack.List(r.Key(), restic.ReaderAt(ctx, r.Backend(), h), size)
if err != nil { if err != nil {
return nil, 0, err return nil, 0, err
} }

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 // 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. // start with prefix. If none is found, nil and ErrNoIDPrefixFound is returned.
// If more than one is found, nil and ErrMultipleIDMatches 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 := "" match := ""
ctx, cancel := context.WithCancel(context.TODO()) ctx, cancel := context.WithCancel(ctx)
defer cancel() defer cancel()
err := be.List(ctx, t, func(fi FileInfo) error { 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 // PrefixLength returns the number of bytes required so that all prefixes of
// all names of type t are unique. // 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 // load all IDs of the given type
list := make([]string, 0, 100) list := make([]string, 0, 100)
ctx, cancel := context.WithCancel(context.TODO()) ctx, cancel := context.WithCancel(ctx)
defer cancel() defer cancel()
err := be.List(ctx, t, func(fi FileInfo) error { err := be.List(ctx, t, func(fi FileInfo) error {

View File

@ -38,7 +38,7 @@ func TestFind(t *testing.T) {
return nil return nil
} }
f, err := Find(m, SnapshotFile, "20bdc1402a6fc9b633aa") f, err := Find(context.TODO(), m, SnapshotFile, "20bdc1402a6fc9b633aa")
if err != nil { if err != nil {
t.Error(err) t.Error(err)
} }
@ -47,7 +47,7 @@ func TestFind(t *testing.T) {
t.Errorf("Wrong match returned want %s, got %s", expectedMatch, f) 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 { if err != ErrNoIDPrefixFound {
t.Error("Expected no snapshots to be found.") 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. // Try to match with a prefix longer than any ID.
extraLengthID := samples[0].String() + "f" extraLengthID := samples[0].String() + "f"
f, err = Find(m, SnapshotFile, extraLengthID) f, err = Find(context.TODO(), m, SnapshotFile, extraLengthID)
if err != ErrNoIDPrefixFound { if err != ErrNoIDPrefixFound {
t.Error("Expected no snapshots to be matched.") 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`. // 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 { if err != ErrMultipleIDMatches {
t.Error("Expected multiple snapshots to be matched.") t.Error("Expected multiple snapshots to be matched.")
} }
@ -89,7 +89,7 @@ func TestPrefixLength(t *testing.T) {
return nil return nil
} }
l, err := PrefixLength(m, SnapshotFile) l, err := PrefixLength(context.TODO(), m, SnapshotFile)
if err != nil { if err != nil {
t.Error(err) t.Error(err)
} }
@ -98,7 +98,7 @@ func TestPrefixLength(t *testing.T) {
} }
list = samples[:3] list = samples[:3]
l, err = PrefixLength(m, SnapshotFile) l, err = PrefixLength(context.TODO(), m, SnapshotFile)
if err != nil { if err != nil {
t.Error(err) t.Error(err)
} }
@ -107,7 +107,7 @@ func TestPrefixLength(t *testing.T) {
} }
list = samples[3:] list = samples[3:]
l, err = PrefixLength(m, SnapshotFile) l, err = PrefixLength(context.TODO(), m, SnapshotFile)
if err != nil { if err != nil {
t.Error(err) t.Error(err)
} }

View File

@ -284,7 +284,7 @@ func RemoveStaleLocks(ctx context.Context, repo Repository) error {
} }
if lock.Stale() { if lock.Stale() {
return repo.Backend().Remove(context.TODO(), Handle{Type: LockFile, Name: id.String()}) return repo.Backend().Remove(ctx, Handle{Type: LockFile, Name: id.String()})
} }
return nil return nil
@ -294,6 +294,6 @@ func RemoveStaleLocks(ctx context.Context, repo Repository) error {
// RemoveAllLocks removes all locks forcefully. // RemoveAllLocks removes all locks forcefully.
func RemoveAllLocks(ctx context.Context, repo Repository) error { func RemoveAllLocks(ctx context.Context, repo Repository) error {
return repo.List(ctx, LockFile, func(id ID, size int64) error { return repo.List(ctx, LockFile, func(id ID, size int64) error {
return repo.Backend().Remove(context.TODO(), Handle{Type: LockFile, Name: id.String()}) return repo.Backend().Remove(ctx, Handle{Type: LockFile, Name: id.String()})
}) })
} }

View File

@ -9,17 +9,20 @@ import (
) )
type backendReaderAt struct { type backendReaderAt struct {
be Backend ctx context.Context
h Handle be Backend
h Handle
} }
func (brd backendReaderAt) ReadAt(p []byte, offset int64) (n int, err error) { func (brd backendReaderAt) ReadAt(p []byte, offset int64) (n int, err error) {
return ReadAt(context.TODO(), brd.be, brd.h, offset, p) return ReadAt(brd.ctx, brd.be, brd.h, offset, p)
} }
// ReaderAt returns an io.ReaderAt for a file in the backend. // ReaderAt returns an io.ReaderAt for a file in the backend. The returned reader
func ReaderAt(be Backend, h Handle) io.ReaderAt { // should not escape the caller function to avoid unexpected interactions with the
return backendReaderAt{be: be, h: h} // embedded context
func ReaderAt(ctx context.Context, be Backend, h Handle) io.ReaderAt {
return backendReaderAt{ctx: ctx, be: be, h: h}
} }
// ReadAt reads from the backend handle h at the given position. // ReadAt reads from the backend handle h at the given position.

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 // FindSnapshot takes a string and tries to find a snapshot whose ID matches
// the string as closely as possible. // 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 // find snapshot id with prefix
name, err := Find(repo.Backend(), SnapshotFile, s) name, err := Find(ctx, repo.Backend(), SnapshotFile, s)
if err != nil { if err != nil {
return ID{}, err return ID{}, err
} }

View File

@ -179,26 +179,26 @@ func TestFileRestorerBasic(t *testing.T) {
defer cleanup() defer cleanup()
restoreAndVerify(t, tempdir, []TestFile{ restoreAndVerify(t, tempdir, []TestFile{
TestFile{ {
name: "file1", name: "file1",
blobs: []TestBlob{ blobs: []TestBlob{
TestBlob{"data1-1", "pack1-1"}, {"data1-1", "pack1-1"},
TestBlob{"data1-2", "pack1-2"}, {"data1-2", "pack1-2"},
}, },
}, },
TestFile{ {
name: "file2", name: "file2",
blobs: []TestBlob{ blobs: []TestBlob{
TestBlob{"data2-1", "pack2-1"}, {"data2-1", "pack2-1"},
TestBlob{"data2-2", "pack2-2"}, {"data2-2", "pack2-2"},
}, },
}, },
TestFile{ {
name: "file3", name: "file3",
blobs: []TestBlob{ blobs: []TestBlob{
// same blob multiple times // same blob multiple times
TestBlob{"data3-1", "pack3-1"}, {"data3-1", "pack3-1"},
TestBlob{"data3-1", "pack3-1"}, {"data3-1", "pack3-1"},
}, },
}, },
}) })

View File

@ -24,7 +24,7 @@ type Restorer struct {
var restorerAbortOnAllErrors = func(location string, err error) error { return err } var restorerAbortOnAllErrors = func(location string, err error) error { return err }
// NewRestorer creates a restorer preloaded with the content from the snapshot id. // NewRestorer creates a restorer preloaded with the content from the snapshot id.
func NewRestorer(repo restic.Repository, id restic.ID) (*Restorer, error) { func NewRestorer(ctx context.Context, repo restic.Repository, id restic.ID) (*Restorer, error) {
r := &Restorer{ r := &Restorer{
repo: repo, repo: repo,
Error: restorerAbortOnAllErrors, Error: restorerAbortOnAllErrors,
@ -33,7 +33,7 @@ func NewRestorer(repo restic.Repository, id restic.ID) (*Restorer, error) {
var err error var err error
r.sn, err = restic.LoadSnapshot(context.TODO(), repo, id) r.sn, err = restic.LoadSnapshot(ctx, repo, id)
if err != nil { if err != nil {
return nil, err return nil, err
} }

View File

@ -326,7 +326,7 @@ func TestRestorer(t *testing.T) {
_, id := saveSnapshot(t, repo, test.Snapshot) _, id := saveSnapshot(t, repo, test.Snapshot)
t.Logf("snapshot saved as %v", id.Str()) t.Logf("snapshot saved as %v", id.Str())
res, err := NewRestorer(repo, id) res, err := NewRestorer(context.TODO(), repo, id)
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
} }
@ -444,7 +444,7 @@ func TestRestorerRelative(t *testing.T) {
_, id := saveSnapshot(t, repo, test.Snapshot) _, id := saveSnapshot(t, repo, test.Snapshot)
t.Logf("snapshot saved as %v", id.Str()) t.Logf("snapshot saved as %v", id.Str())
res, err := NewRestorer(repo, id) res, err := NewRestorer(context.TODO(), repo, id)
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
} }
@ -676,7 +676,7 @@ func TestRestorerTraverseTree(t *testing.T) {
defer cleanup() defer cleanup()
sn, id := saveSnapshot(t, repo, test.Snapshot) sn, id := saveSnapshot(t, repo, test.Snapshot)
res, err := NewRestorer(repo, id) res, err := NewRestorer(context.TODO(), repo, id)
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
} }

View File

@ -29,7 +29,7 @@ func TestRestorerRestoreEmptyHardlinkedFileds(t *testing.T) {
}, },
}) })
res, err := NewRestorer(repo, id) res, err := NewRestorer(context.TODO(), repo, id)
rtest.OK(t, err) rtest.OK(t, err)
res.SelectFilter = func(item string, dstpath string, node *restic.Node) (selectedForRestore bool, childMayBeSelected bool) { res.SelectFilter = func(item string, dstpath string, node *restic.Node) (selectedForRestore bool, childMayBeSelected bool) {