mirror of
https://github.com/rclone/rclone.git
synced 2025-12-11 22:14:05 +01: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
@@ -2,6 +2,7 @@ package touch
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"time"
|
||||
|
||||
"github.com/ncw/rclone/cmd"
|
||||
@@ -33,13 +34,13 @@ var commandDefintion = &cobra.Command{
|
||||
cmd.CheckArgs(1, 1, command, args)
|
||||
fsrc, srcFileName := cmd.NewFsDstFile(args)
|
||||
cmd.Run(true, false, command, func() error {
|
||||
return Touch(fsrc, srcFileName)
|
||||
return Touch(context.Background(), fsrc, srcFileName)
|
||||
})
|
||||
},
|
||||
}
|
||||
|
||||
//Touch create new file or change file modification time.
|
||||
func Touch(fsrc fs.Fs, srcFileName string) error {
|
||||
func Touch(ctx context.Context, fsrc fs.Fs, srcFileName string) error {
|
||||
timeAtr := time.Now()
|
||||
if timeAsArgument != "" {
|
||||
layout := defaultLayout
|
||||
@@ -52,19 +53,19 @@ func Touch(fsrc fs.Fs, srcFileName string) error {
|
||||
}
|
||||
timeAtr = timeAtrFromFlags
|
||||
}
|
||||
file, err := fsrc.NewObject(srcFileName)
|
||||
file, err := fsrc.NewObject(ctx, srcFileName)
|
||||
if err != nil {
|
||||
if !notCreateNewFile {
|
||||
var buffer []byte
|
||||
src := object.NewStaticObjectInfo(srcFileName, timeAtr, int64(len(buffer)), true, nil, fsrc)
|
||||
_, err = fsrc.Put(bytes.NewBuffer(buffer), src)
|
||||
_, err = fsrc.Put(ctx, bytes.NewBuffer(buffer), src)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
err = file.SetModTime(timeAtr)
|
||||
err = file.SetModTime(ctx, timeAtr)
|
||||
if err != nil {
|
||||
return errors.Wrap(err, "touch: couldn't set mod time")
|
||||
}
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
package touch
|
||||
|
||||
import (
|
||||
"context"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
@@ -34,9 +35,9 @@ func TestTouchOneFile(t *testing.T) {
|
||||
r := fstest.NewRun(t)
|
||||
defer r.Finalise()
|
||||
|
||||
err := Touch(r.Fremote, "newFile")
|
||||
err := Touch(context.Background(), r.Fremote, "newFile")
|
||||
require.NoError(t, err)
|
||||
_, err = r.Fremote.NewObject("newFile")
|
||||
_, err = r.Fremote.NewObject(context.Background(), "newFile")
|
||||
require.NoError(t, err)
|
||||
}
|
||||
|
||||
@@ -45,9 +46,9 @@ func TestTouchWithNoCreateFlag(t *testing.T) {
|
||||
defer r.Finalise()
|
||||
|
||||
notCreateNewFile = true
|
||||
err := Touch(r.Fremote, "newFile")
|
||||
err := Touch(context.Background(), r.Fremote, "newFile")
|
||||
require.NoError(t, err)
|
||||
_, err = r.Fremote.NewObject("newFile")
|
||||
_, err = r.Fremote.NewObject(context.Background(), "newFile")
|
||||
require.Error(t, err)
|
||||
notCreateNewFile = false
|
||||
}
|
||||
@@ -58,7 +59,7 @@ func TestTouchWithTimestamp(t *testing.T) {
|
||||
|
||||
timeAsArgument = "060102"
|
||||
srcFileName := "oldFile"
|
||||
err := Touch(r.Fremote, srcFileName)
|
||||
err := Touch(context.Background(), r.Fremote, srcFileName)
|
||||
require.NoError(t, err)
|
||||
checkFile(t, r.Fremote, srcFileName, "")
|
||||
}
|
||||
@@ -69,7 +70,7 @@ func TestTouchWithLognerTimestamp(t *testing.T) {
|
||||
|
||||
timeAsArgument = "2006-01-02T15:04:05"
|
||||
srcFileName := "oldFile"
|
||||
err := Touch(r.Fremote, srcFileName)
|
||||
err := Touch(context.Background(), r.Fremote, srcFileName)
|
||||
require.NoError(t, err)
|
||||
checkFile(t, r.Fremote, srcFileName, "")
|
||||
}
|
||||
@@ -80,11 +81,11 @@ func TestTouchUpdateTimestamp(t *testing.T) {
|
||||
|
||||
srcFileName := "a"
|
||||
content := "aaa"
|
||||
file1 := r.WriteObject(srcFileName, content, t1)
|
||||
file1 := r.WriteObject(context.Background(), srcFileName, content, t1)
|
||||
fstest.CheckItems(t, r.Fremote, file1)
|
||||
|
||||
timeAsArgument = "121212"
|
||||
err := Touch(r.Fremote, "a")
|
||||
err := Touch(context.Background(), r.Fremote, "a")
|
||||
require.NoError(t, err)
|
||||
checkFile(t, r.Fremote, srcFileName, content)
|
||||
}
|
||||
@@ -95,12 +96,12 @@ func TestTouchUpdateTimestampWithCFlag(t *testing.T) {
|
||||
|
||||
srcFileName := "a"
|
||||
content := "aaa"
|
||||
file1 := r.WriteObject(srcFileName, content, t1)
|
||||
file1 := r.WriteObject(context.Background(), srcFileName, content, t1)
|
||||
fstest.CheckItems(t, r.Fremote, file1)
|
||||
|
||||
notCreateNewFile = true
|
||||
timeAsArgument = "121212"
|
||||
err := Touch(r.Fremote, "a")
|
||||
err := Touch(context.Background(), r.Fremote, "a")
|
||||
require.NoError(t, err)
|
||||
checkFile(t, r.Fremote, srcFileName, content)
|
||||
notCreateNewFile = false
|
||||
@@ -111,7 +112,7 @@ func TestTouchCreateMultipleDirAndFile(t *testing.T) {
|
||||
defer r.Finalise()
|
||||
|
||||
longPath := "a/b/c.txt"
|
||||
err := Touch(r.Fremote, longPath)
|
||||
err := Touch(context.Background(), r.Fremote, longPath)
|
||||
require.NoError(t, err)
|
||||
file1 := fstest.NewItem("a/b/c.txt", "", t1)
|
||||
fstest.CheckListingWithPrecision(t, r.Fremote, []fstest.Item{file1}, []string{"a", "a/b"}, fs.ModTimeNotSupported)
|
||||
|
||||
Reference in New Issue
Block a user