mirror of
https://github.com/rclone/rclone.git
synced 2025-12-06 00:03:32 +00:00
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
@@ -1,6 +1,7 @@
|
||||
package operations
|
||||
|
||||
import (
|
||||
"context"
|
||||
"strings"
|
||||
|
||||
"github.com/ncw/rclone/fs"
|
||||
@@ -36,7 +37,7 @@ See the [lsjson command](/commands/rclone_lsjson/) for more information on the a
|
||||
}
|
||||
|
||||
// List the directory
|
||||
func rcList(in rc.Params) (out rc.Params, err error) {
|
||||
func rcList(ctx context.Context, in rc.Params) (out rc.Params, err error) {
|
||||
f, remote, err := rc.GetFsAndRemote(in)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
@@ -47,7 +48,7 @@ func rcList(in rc.Params) (out rc.Params, err error) {
|
||||
return nil, err
|
||||
}
|
||||
var list = []*ListJSONItem{}
|
||||
err = ListJSON(f, remote, &opt, func(item *ListJSONItem) error {
|
||||
err = ListJSON(ctx, f, remote, &opt, func(item *ListJSONItem) error {
|
||||
list = append(list, item)
|
||||
return nil
|
||||
})
|
||||
@@ -77,7 +78,7 @@ See the [about command](/commands/rclone_size/) command for more information on
|
||||
}
|
||||
|
||||
// About the remote
|
||||
func rcAbout(in rc.Params) (out rc.Params, err error) {
|
||||
func rcAbout(ctx context.Context, in rc.Params) (out rc.Params, err error) {
|
||||
f, err := rc.GetFs(in)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
@@ -86,7 +87,7 @@ func rcAbout(in rc.Params) (out rc.Params, err error) {
|
||||
if doAbout == nil {
|
||||
return nil, errors.Errorf("%v doesn't support about", f)
|
||||
}
|
||||
u, err := doAbout()
|
||||
u, err := doAbout(ctx)
|
||||
if err != nil {
|
||||
return nil, errors.Wrap(err, "about call failed")
|
||||
}
|
||||
@@ -107,8 +108,8 @@ func init() {
|
||||
rc.Add(rc.Call{
|
||||
Path: "operations/" + strings.ToLower(name) + "file",
|
||||
AuthRequired: true,
|
||||
Fn: func(in rc.Params) (rc.Params, error) {
|
||||
return rcMoveOrCopyFile(in, copy)
|
||||
Fn: func(ctx context.Context, in rc.Params) (rc.Params, error) {
|
||||
return rcMoveOrCopyFile(ctx, in, copy)
|
||||
},
|
||||
Title: name + " a file from source remote to destination remote",
|
||||
Help: `This takes the following parameters
|
||||
@@ -123,7 +124,7 @@ func init() {
|
||||
}
|
||||
|
||||
// Copy a file
|
||||
func rcMoveOrCopyFile(in rc.Params, cp bool) (out rc.Params, err error) {
|
||||
func rcMoveOrCopyFile(ctx context.Context, in rc.Params, cp bool) (out rc.Params, err error) {
|
||||
srcFs, srcRemote, err := rc.GetFsAndRemoteNamed(in, "srcFs", "srcRemote")
|
||||
if err != nil {
|
||||
return nil, err
|
||||
@@ -132,7 +133,7 @@ func rcMoveOrCopyFile(in rc.Params, cp bool) (out rc.Params, err error) {
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return nil, moveOrCopyFile(dstFs, srcFs, dstRemote, srcRemote, cp)
|
||||
return nil, moveOrCopyFile(ctx, dstFs, srcFs, dstRemote, srcRemote, cp)
|
||||
}
|
||||
|
||||
func init() {
|
||||
@@ -159,8 +160,8 @@ func init() {
|
||||
rc.Add(rc.Call{
|
||||
Path: "operations/" + op.name,
|
||||
AuthRequired: true,
|
||||
Fn: func(in rc.Params) (rc.Params, error) {
|
||||
return rcSingleCommand(in, op.name, op.noRemote)
|
||||
Fn: func(ctx context.Context, in rc.Params) (rc.Params, error) {
|
||||
return rcSingleCommand(ctx, in, op.name, op.noRemote)
|
||||
},
|
||||
Title: op.title,
|
||||
Help: `This takes the following parameters
|
||||
@@ -174,7 +175,7 @@ See the [` + op.name + ` command](/commands/rclone_` + op.name + `/) command for
|
||||
}
|
||||
|
||||
// Run a single command, eg Mkdir
|
||||
func rcSingleCommand(in rc.Params, name string, noRemote bool) (out rc.Params, err error) {
|
||||
func rcSingleCommand(ctx context.Context, in rc.Params, name string, noRemote bool) (out rc.Params, err error) {
|
||||
var (
|
||||
f fs.Fs
|
||||
remote string
|
||||
@@ -189,34 +190,34 @@ func rcSingleCommand(in rc.Params, name string, noRemote bool) (out rc.Params, e
|
||||
}
|
||||
switch name {
|
||||
case "mkdir":
|
||||
return nil, Mkdir(f, remote)
|
||||
return nil, Mkdir(ctx, f, remote)
|
||||
case "rmdir":
|
||||
return nil, Rmdir(f, remote)
|
||||
return nil, Rmdir(ctx, f, remote)
|
||||
case "purge":
|
||||
return nil, Purge(f, remote)
|
||||
return nil, Purge(ctx, f, remote)
|
||||
case "rmdirs":
|
||||
leaveRoot, err := in.GetBool("leaveRoot")
|
||||
if rc.NotErrParamNotFound(err) {
|
||||
return nil, err
|
||||
}
|
||||
return nil, Rmdirs(f, remote, leaveRoot)
|
||||
return nil, Rmdirs(ctx, f, remote, leaveRoot)
|
||||
case "delete":
|
||||
return nil, Delete(f)
|
||||
return nil, Delete(ctx, f)
|
||||
case "deletefile":
|
||||
o, err := f.NewObject(remote)
|
||||
o, err := f.NewObject(ctx, remote)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return nil, DeleteFile(o)
|
||||
return nil, DeleteFile(ctx, o)
|
||||
case "copyurl":
|
||||
url, err := in.GetString("url")
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
_, err = CopyURL(f, remote, url)
|
||||
_, err = CopyURL(ctx, f, remote, url)
|
||||
return nil, err
|
||||
case "cleanup":
|
||||
return nil, CleanUp(f)
|
||||
return nil, CleanUp(ctx, f)
|
||||
}
|
||||
panic("unknown rcSingleCommand type")
|
||||
}
|
||||
@@ -242,12 +243,12 @@ See the [size command](/commands/rclone_size/) command for more information on t
|
||||
}
|
||||
|
||||
// Size a directory
|
||||
func rcSize(in rc.Params) (out rc.Params, err error) {
|
||||
func rcSize(ctx context.Context, in rc.Params) (out rc.Params, err error) {
|
||||
f, err := rc.GetFs(in)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
count, bytes, err := Count(f)
|
||||
count, bytes, err := Count(ctx, f)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@@ -278,12 +279,12 @@ See the [link command](/commands/rclone_link/) command for more information on t
|
||||
}
|
||||
|
||||
// Make a public link
|
||||
func rcPublicLink(in rc.Params) (out rc.Params, err error) {
|
||||
func rcPublicLink(ctx context.Context, in rc.Params) (out rc.Params, err error) {
|
||||
f, remote, err := rc.GetFsAndRemote(in)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
url, err := PublicLink(f, remote)
|
||||
url, err := PublicLink(ctx, f, remote)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@@ -357,7 +358,7 @@ This command does not have a command line equivalent so use this instead:
|
||||
}
|
||||
|
||||
// Fsinfo the remote
|
||||
func rcFsInfo(in rc.Params) (out rc.Params, err error) {
|
||||
func rcFsInfo(ctx context.Context, in rc.Params) (out rc.Params, err error) {
|
||||
f, err := rc.GetFs(in)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
|
||||
Reference in New Issue
Block a user