mirror of
https://github.com/restic/restic.git
synced 2025-08-12 11:27:46 +00:00
Merge pull request #2933 from MichaelEischer/less-context-todo
Replace most usages of context.TODO()
This commit is contained in:
@@ -275,8 +275,8 @@ func Open(cfg Config, lim limiter.Limiter) (*Backend, error) {
|
||||
return be, nil
|
||||
}
|
||||
|
||||
// Create initializes a new restic repo with clone.
|
||||
func Create(cfg Config) (*Backend, error) {
|
||||
// Create initializes a new restic repo with rclone.
|
||||
func Create(ctx context.Context, cfg Config) (*Backend, error) {
|
||||
be, err := newBackend(cfg, nil)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
@@ -294,7 +294,7 @@ func Create(cfg Config) (*Backend, error) {
|
||||
URL: url,
|
||||
}
|
||||
|
||||
restBackend, err := rest.Create(restConfig, debug.RoundTripper(be.tr))
|
||||
restBackend, err := rest.Create(ctx, restConfig, debug.RoundTripper(be.tr))
|
||||
if err != nil {
|
||||
_ = be.Close()
|
||||
return nil, err
|
||||
|
@@ -1,6 +1,7 @@
|
||||
package rclone_test
|
||||
|
||||
import (
|
||||
"context"
|
||||
"os/exec"
|
||||
"testing"
|
||||
|
||||
@@ -27,7 +28,7 @@ func newTestSuite(t testing.TB) *test.Suite {
|
||||
Create: func(config interface{}) (restic.Backend, error) {
|
||||
t.Logf("Create()")
|
||||
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 {
|
||||
t.Skipf("program %q not found", e.Name)
|
||||
return nil, nil
|
||||
|
@@ -63,13 +63,13 @@ func Open(cfg Config, rt http.RoundTripper) (*Backend, error) {
|
||||
}
|
||||
|
||||
// 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)
|
||||
if err != nil {
|
||||
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 {
|
||||
return nil, errors.Fatal("config file already exists")
|
||||
}
|
||||
|
@@ -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.
|
||||
Create: func(config interface{}) (restic.Backend, error) {
|
||||
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.
|
||||
|
@@ -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)
|
||||
}
|
||||
|
@@ -128,7 +128,7 @@ func TestUnpackReadSeeker(t *testing.T) {
|
||||
|
||||
handle := restic.Handle{Type: restic.PackFile, Name: id.String()}
|
||||
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) {
|
||||
@@ -141,5 +141,5 @@ func TestShortPack(t *testing.T) {
|
||||
|
||||
handle := restic.Handle{Type: restic.PackFile, Name: id.String()}
|
||||
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)
|
||||
}
|
||||
|
@@ -57,8 +57,8 @@ var (
|
||||
|
||||
// createMasterKey creates a new master key in the given backend and encrypts
|
||||
// it with the password.
|
||||
func createMasterKey(s *Repository, password string) (*Key, error) {
|
||||
return AddKey(context.TODO(), s, password, "", "", nil)
|
||||
func createMasterKey(ctx context.Context, s *Repository, password string) (*Key, error) {
|
||||
return AddKey(ctx, s, password, "", "", nil)
|
||||
}
|
||||
|
||||
// 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
|
||||
|
||||
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)
|
||||
|
@@ -267,7 +267,7 @@ func (mi *MasterIndex) MergeFinalIndexes() {
|
||||
// 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
|
||||
// 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()
|
||||
defer mi.idxMutex.Unlock()
|
||||
|
||||
@@ -275,7 +275,7 @@ func (mi *MasterIndex) RebuildIndex(packBlacklist restic.IDSet) (*Index, error)
|
||||
|
||||
newIndex := NewIndex()
|
||||
|
||||
ctx, cancel := context.WithCancel(context.TODO())
|
||||
ctx, cancel := context.WithCancel(ctx)
|
||||
defer cancel()
|
||||
|
||||
for i, idx := range mi.idx {
|
||||
|
@@ -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
|
||||
@@ -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
|
||||
// the config into the repo.
|
||||
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 {
|
||||
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) {
|
||||
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 {
|
||||
return nil, 0, err
|
||||
}
|
||||
|
@@ -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 {
|
||||
|
@@ -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)
|
||||
}
|
||||
|
@@ -284,7 +284,7 @@ func RemoveStaleLocks(ctx context.Context, repo Repository) error {
|
||||
}
|
||||
|
||||
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
|
||||
@@ -294,6 +294,6 @@ func RemoveStaleLocks(ctx context.Context, repo Repository) error {
|
||||
// RemoveAllLocks removes all locks forcefully.
|
||||
func RemoveAllLocks(ctx context.Context, repo Repository) 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()})
|
||||
})
|
||||
}
|
||||
|
@@ -9,17 +9,20 @@ import (
|
||||
)
|
||||
|
||||
type backendReaderAt struct {
|
||||
be Backend
|
||||
h Handle
|
||||
ctx context.Context
|
||||
be Backend
|
||||
h Handle
|
||||
}
|
||||
|
||||
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.
|
||||
func ReaderAt(be Backend, h Handle) io.ReaderAt {
|
||||
return backendReaderAt{be: be, h: h}
|
||||
// ReaderAt returns an io.ReaderAt for a file in the backend. The returned reader
|
||||
// should not escape the caller function to avoid unexpected interactions with the
|
||||
// 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.
|
||||
|
@@ -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
|
||||
}
|
||||
|
@@ -179,26 +179,26 @@ func TestFileRestorerBasic(t *testing.T) {
|
||||
defer cleanup()
|
||||
|
||||
restoreAndVerify(t, tempdir, []TestFile{
|
||||
TestFile{
|
||||
{
|
||||
name: "file1",
|
||||
blobs: []TestBlob{
|
||||
TestBlob{"data1-1", "pack1-1"},
|
||||
TestBlob{"data1-2", "pack1-2"},
|
||||
{"data1-1", "pack1-1"},
|
||||
{"data1-2", "pack1-2"},
|
||||
},
|
||||
},
|
||||
TestFile{
|
||||
{
|
||||
name: "file2",
|
||||
blobs: []TestBlob{
|
||||
TestBlob{"data2-1", "pack2-1"},
|
||||
TestBlob{"data2-2", "pack2-2"},
|
||||
{"data2-1", "pack2-1"},
|
||||
{"data2-2", "pack2-2"},
|
||||
},
|
||||
},
|
||||
TestFile{
|
||||
{
|
||||
name: "file3",
|
||||
blobs: []TestBlob{
|
||||
// same blob multiple times
|
||||
TestBlob{"data3-1", "pack3-1"},
|
||||
TestBlob{"data3-1", "pack3-1"},
|
||||
{"data3-1", "pack3-1"},
|
||||
{"data3-1", "pack3-1"},
|
||||
},
|
||||
},
|
||||
})
|
||||
|
@@ -24,7 +24,7 @@ type Restorer struct {
|
||||
var restorerAbortOnAllErrors = func(location string, err error) error { return err }
|
||||
|
||||
// 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{
|
||||
repo: repo,
|
||||
Error: restorerAbortOnAllErrors,
|
||||
@@ -33,7 +33,7 @@ func NewRestorer(repo restic.Repository, id restic.ID) (*Restorer, error) {
|
||||
|
||||
var err error
|
||||
|
||||
r.sn, err = restic.LoadSnapshot(context.TODO(), repo, id)
|
||||
r.sn, err = restic.LoadSnapshot(ctx, repo, id)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
@@ -326,7 +326,7 @@ func TestRestorer(t *testing.T) {
|
||||
_, id := saveSnapshot(t, repo, test.Snapshot)
|
||||
t.Logf("snapshot saved as %v", id.Str())
|
||||
|
||||
res, err := NewRestorer(repo, id)
|
||||
res, err := NewRestorer(context.TODO(), repo, id)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
@@ -444,7 +444,7 @@ func TestRestorerRelative(t *testing.T) {
|
||||
_, id := saveSnapshot(t, repo, test.Snapshot)
|
||||
t.Logf("snapshot saved as %v", id.Str())
|
||||
|
||||
res, err := NewRestorer(repo, id)
|
||||
res, err := NewRestorer(context.TODO(), repo, id)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
@@ -676,7 +676,7 @@ func TestRestorerTraverseTree(t *testing.T) {
|
||||
defer cleanup()
|
||||
sn, id := saveSnapshot(t, repo, test.Snapshot)
|
||||
|
||||
res, err := NewRestorer(repo, id)
|
||||
res, err := NewRestorer(context.TODO(), repo, id)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
@@ -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)
|
||||
|
||||
res.SelectFilter = func(item string, dstpath string, node *restic.Node) (selectedForRestore bool, childMayBeSelected bool) {
|
||||
|
Reference in New Issue
Block a user