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: stop --copy-dest replacing files with --immutable

phatlcdc98c216f2c8
diff --git a/fs/operations/copy_test.go b/fs/operations/copy_test.go
index f62498dab..dfd0255b9 100644
--- a/fs/operations/copy_test.go
+++ b/fs/operations/copy_test.go
@@ -301,6 +301,35 @@ func TestCopyFileCompareDest(t *testing.T) {
r.CheckRemoteItems(t, file2, file3, file4, file5bdst)
}
+// Test with CopyDest and Immutable set
+func TestCopyFileCopyDestImmutable(t *testing.T) {
+ ctx := context.Background()
+ ctx, ci := fs.AddConfig(ctx)
+ r := fstest.NewRun(t)
+ defer accounting.GlobalStats().ResetCounters()
+
+ if r.Fremote.Features().Copy == nil {
+ t.Skip("Skipping test as remote does not support server-side copy")
+ }
+
+ ci.CopyDest = []string{r.FremoteName + "/CopyDest"}
+ ci.Immutable = true
+
+ fdst, err := fs.NewFs(ctx, r.FremoteName+"/dst")
+ require.NoError(t, err)
+
+ file1 := r.WriteObject(ctx, "dst/one", "one", t1)
+ file2 := r.WriteObject(ctx, "CopyDest/one", "onet2", t2)
+ file3 := r.WriteFile("one", "onet2", t2)
+ r.CheckRemoteItems(t, file1, file2)
+
+ // A match in --copy-dest must not replace a different destination
+ err = operations.CopyFile(ctx, fdst, r.Flocal, file3.Path, file3.Path)
+ assert.ErrorIs(t, err, fs.ErrorImmutableModified)
+ r.CheckRemoteItems(t, file1, file2)
+ r.CheckLocalItems(t, file3)
+}
+
// Test with CopyDest set
func TestCopyFileCopyDest(t *testing.T) {
ctx := context.Background()
diff --git a/fs/operations/operations.go b/fs/operations/operations.go
index eff2c6ae0..c09439086 100644
--- a/fs/operations/operations.go
+++ b/fs/operations/operations.go
@@ -1721,6 +1721,10 @@ func copyDest(ctx context.Context, fdst fs.Fs, dst, src fs.Object, CopyDest, bac
opt.updateModTime = false
if equal(ctx, src, CopyDestFile, opt) {
if dst == nil || !Equal(ctx, src, dst) {
+ // Leave a different destination for the caller to reject
+ if dst != nil && fs.GetConfig(ctx).Immutable {
+ return false, nil
+ }
if dst != nil && backupDir != nil {
err = MoveBackupDir(ctx, backupDir, dst)
if err != nil {
diff --git a/fs/sync/sync_test.go b/fs/sync/sync_test.go
index 09366cf65..b7a140bad 100644
--- a/fs/sync/sync_test.go
+++ b/fs/sync/sync_test.go
@@ -2256,6 +2256,36 @@ func TestSyncMultipleCompareDest(t *testing.T) {
r.CheckRemoteItems(t, fdest1, fdest2, fdest3)
}
+// Test with CopyDest and Immutable set
+func TestSyncCopyDestImmutable(t *testing.T) {
+ ctx := context.Background()
+ ctx, ci := fs.AddConfig(ctx)
+ r := fstest.NewRun(t)
+ defer accounting.GlobalStats().ResetCounters()
+
+ if r.Fremote.Features().Copy == nil {
+ t.Skip("Skipping test as remote does not support server-side copy")
+ }
+
+ ci.CopyDest = []string{r.FremoteName + "/CopyDest"}
+ ci.Immutable = true
+
+ fdst, err := fs.NewFs(ctx, r.FremoteName+"/dst")
+ require.NoError(t, err)
+
+ file1 := r.WriteObject(ctx, "dst/one", "one", t1)
+ file2 := r.WriteObject(ctx, "CopyDest/one", "onet2", t2)
+ file3 := r.WriteFile("one", "onet2", t2)
+ r.CheckRemoteItems(t, file1, file2)
+
+ // A match in --copy-dest must not replace a different destination
+ accounting.GlobalStats().ResetCounters()
+ err = CopyDir(ctx, fdst, r.Flocal, false)
+ assert.EqualError(t, err, fs.ErrorImmutableModified.Error())
+ r.CheckRemoteItems(t, file1, file2)
+ r.CheckLocalItems(t, file3)
+}
+
// Test with CopyDest set
func TestSyncCopyDest(t *testing.T) {
ctx := context.Background()