Add context propagation to rclone
- Change rclone/fs interfaces to accept context.Context - Update interface implementations to use context.Context - Change top level usage to propagate context to lover level functions Context propagation is needed for stopping transfers and passing other request-scoped values.
This commit is contained in:
committed by
Nick Craig-Wood
parent
a2c317b46e
commit
f78cd1e043
+16
-15
@@ -4,6 +4,7 @@ package dircache
|
||||
// _methods are called without the lock
|
||||
|
||||
import (
|
||||
"context"
|
||||
"log"
|
||||
"strings"
|
||||
"sync"
|
||||
@@ -28,8 +29,8 @@ type DirCache struct {
|
||||
|
||||
// DirCacher describes an interface for doing the low level directory work
|
||||
type DirCacher interface {
|
||||
FindLeaf(pathID, leaf string) (pathIDOut string, found bool, err error)
|
||||
CreateDir(pathID, leaf string) (newID string, err error)
|
||||
FindLeaf(ctx context.Context, pathID, leaf string) (pathIDOut string, found bool, err error)
|
||||
CreateDir(ctx context.Context, pathID, leaf string) (newID string, err error)
|
||||
}
|
||||
|
||||
// New makes a DirCache
|
||||
@@ -135,10 +136,10 @@ func SplitPath(path string) (directory, leaf string) {
|
||||
// Look in the cache for the path, if found return the pathID
|
||||
// If not found strip the last path off the path and recurse
|
||||
// Now have a parent directory id, so look in the parent for self and return it
|
||||
func (dc *DirCache) FindDir(path string, create bool) (pathID string, err error) {
|
||||
func (dc *DirCache) FindDir(ctx context.Context, path string, create bool) (pathID string, err error) {
|
||||
dc.mu.Lock()
|
||||
defer dc.mu.Unlock()
|
||||
return dc._findDir(path, create)
|
||||
return dc._findDir(ctx, path, create)
|
||||
}
|
||||
|
||||
// Look for the root and in the cache - safe to call without the mu
|
||||
@@ -161,7 +162,7 @@ func (dc *DirCache) _findDirInCache(path string) string {
|
||||
}
|
||||
|
||||
// Unlocked findDir - must have mu
|
||||
func (dc *DirCache) _findDir(path string, create bool) (pathID string, err error) {
|
||||
func (dc *DirCache) _findDir(ctx context.Context, path string, create bool) (pathID string, err error) {
|
||||
pathID = dc._findDirInCache(path)
|
||||
if pathID != "" {
|
||||
return pathID, nil
|
||||
@@ -171,14 +172,14 @@ func (dc *DirCache) _findDir(path string, create bool) (pathID string, err error
|
||||
directory, leaf := SplitPath(path)
|
||||
|
||||
// Recurse and find pathID for parent directory
|
||||
parentPathID, err := dc._findDir(directory, create)
|
||||
parentPathID, err := dc._findDir(ctx, directory, create)
|
||||
if err != nil {
|
||||
return "", err
|
||||
|
||||
}
|
||||
|
||||
// Find the leaf in parentPathID
|
||||
pathID, found, err := dc.fs.FindLeaf(parentPathID, leaf)
|
||||
pathID, found, err := dc.fs.FindLeaf(ctx, parentPathID, leaf)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
@@ -186,7 +187,7 @@ func (dc *DirCache) _findDir(path string, create bool) (pathID string, err error
|
||||
// If not found create the directory if required or return an error
|
||||
if !found {
|
||||
if create {
|
||||
pathID, err = dc.fs.CreateDir(parentPathID, leaf)
|
||||
pathID, err = dc.fs.CreateDir(ctx, parentPathID, leaf)
|
||||
if err != nil {
|
||||
return "", errors.Wrap(err, "failed to make directory")
|
||||
}
|
||||
@@ -207,7 +208,7 @@ func (dc *DirCache) _findDir(path string, create bool) (pathID string, err error
|
||||
// Do not call FindPath with the root directory - it will return an error
|
||||
//
|
||||
// If create is set parent directories will be created if they don't exist
|
||||
func (dc *DirCache) FindPath(path string, create bool) (leaf, directoryID string, err error) {
|
||||
func (dc *DirCache) FindPath(ctx context.Context, path string, create bool) (leaf, directoryID string, err error) {
|
||||
if path == "" {
|
||||
err = errors.New("internal error: can't call FindPath with root directory")
|
||||
return
|
||||
@@ -215,7 +216,7 @@ func (dc *DirCache) FindPath(path string, create bool) (leaf, directoryID string
|
||||
dc.mu.Lock()
|
||||
defer dc.mu.Unlock()
|
||||
directory, leaf := SplitPath(path)
|
||||
directoryID, err = dc._findDir(directory, create)
|
||||
directoryID, err = dc._findDir(ctx, directory, create)
|
||||
return
|
||||
}
|
||||
|
||||
@@ -224,13 +225,13 @@ func (dc *DirCache) FindPath(path string, create bool) (leaf, directoryID string
|
||||
// Resets the root directory
|
||||
//
|
||||
// If create is set it will make the directory if not found
|
||||
func (dc *DirCache) FindRoot(create bool) error {
|
||||
func (dc *DirCache) FindRoot(ctx context.Context, create bool) error {
|
||||
dc.mu.Lock()
|
||||
defer dc.mu.Unlock()
|
||||
if dc.foundRoot {
|
||||
return nil
|
||||
}
|
||||
rootID, err := dc._findDir(dc.root, create)
|
||||
rootID, err := dc._findDir(ctx, dc.root, create)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
@@ -252,12 +253,12 @@ func (dc *DirCache) FindRoot(create bool) error {
|
||||
// FindRootAndPath finds the root first if not found then finds leaf and directoryID from a path
|
||||
//
|
||||
// If create is set parent directories will be created if they don't exist
|
||||
func (dc *DirCache) FindRootAndPath(path string, create bool) (leaf, directoryID string, err error) {
|
||||
err = dc.FindRoot(create)
|
||||
func (dc *DirCache) FindRootAndPath(ctx context.Context, path string, create bool) (leaf, directoryID string, err error) {
|
||||
err = dc.FindRoot(ctx, create)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
return dc.FindPath(path, create)
|
||||
return dc.FindPath(ctx, path, create)
|
||||
}
|
||||
|
||||
// FoundRoot returns whether the root directory has been found yet
|
||||
|
||||
Reference in New Issue
Block a user