Skip to content

"rsync for cloud storage" - Google Drive, S3, Dropbox, Backblaze B2, One Drive, Swift, Hubic, Wasabi, Google Cloud Storage, Azure Blob, Azure Files, Yandex Files

operations: fix moving a file onto its own name deleting it

nielash9f1d0cf0218e
diff --git a/fs/operations/operations.go b/fs/operations/operations.go
index 52ace4f25..97f82068f 100644
--- a/fs/operations/operations.go
+++ b/fs/operations/operations.go
@@ -32,6 +32,7 @@ import (
"github.com/rclone/rclone/fs/filter"
"github.com/rclone/rclone/fs/fserrors"
"github.com/rclone/rclone/fs/fshttp"
+ "github.com/rclone/rclone/fs/fspath"
"github.com/rclone/rclone/fs/hash"
"github.com/rclone/rclone/fs/object"
"github.com/rclone/rclone/fs/walk"
@@ -436,6 +437,10 @@ func MoveTransfer(ctx context.Context, fdst fs.Fs, dst fs.Object, remote string,
func move(ctx context.Context, fdst fs.Fs, dst fs.Object, remote string, src fs.Object, isTransfer bool) (newDst fs.Object, err error) {
origRemote := remote // avoid double-transform on fallback to copy
remote = transform.Path(ctx, remote, false)
+ if fs.FullPath(src) == fspath.JoinRootPath(fs.ConfigString(fdst), remote) {
+ // nothing to do, and copying an object onto itself then deleting the source would delete it
+ return src, nil
+ }
ci := fs.GetConfig(ctx)
newDst = dst
if ci.DryRun && dst != nil && SameObject(src, dst) && src.Remote() == transform.Path(ctx, dst.Remote(), false) {
diff --git a/fs/operations/operations_test.go b/fs/operations/operations_test.go
index 3a397721d..cc9a487f6 100644
--- a/fs/operations/operations_test.go
+++ b/fs/operations/operations_test.go
@@ -39,6 +39,7 @@ import (
"github.com/rclone/rclone/fs/filter"
"github.com/rclone/rclone/fs/fserrors"
"github.com/rclone/rclone/fs/fshttp"
+ "github.com/rclone/rclone/fs/fspath"
"github.com/rclone/rclone/fs/hash"
"github.com/rclone/rclone/fs/object"
"github.com/rclone/rclone/fs/operations"
@@ -1137,6 +1138,33 @@ func TestCaseInsensitiveMoveFile(t *testing.T) {
r.CheckRemoteItems(t, file2Capitalized)
}
+func TestMoveOntoItself(t *testing.T) {
+ ctx := context.Background()
+ for _, viaSubdir := range []bool{false, true} {
+ for _, disableMove := range []bool{false, true} {
+ t.Run(fmt.Sprintf("viaSubdir=%v,disableMove=%v", viaSubdir, disableMove), func(t *testing.T) {
+ r := fstest.NewRunIndividual(t)
+ file1 := r.WriteObject(ctx, "sub/file1", "file1 contents", t1)
+ src, err := r.Fremote.NewObject(ctx, file1.Path)
+ require.NoError(t, err)
+
+ fdst, remote := r.Fremote, src.Remote()
+ if viaSubdir {
+ fdst, err = fs.NewFs(ctx, fspath.JoinRootPath(fs.ConfigStringFull(r.Fremote), "sub"))
+ require.NoError(t, err)
+ remote = "file1"
+ }
+ if disableMove {
+ fdst.Features().Disable("Move")
+ }
+ _, err = operations.Move(ctx, fdst, nil, remote, src)
+ require.NoError(t, err)
+ r.CheckRemoteItems(t, file1)
+ })
+ }
+ }
+}
+
func TestMoveUnicodeEquivalentWithoutServerSideMove(t *testing.T) {
ctx := context.Background()
r := fstest.NewRunIndividual(t)