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: make --immutable work with copyto, moveto and single file copy - fixes #4921

phatlc575633cf6cd0
diff --git a/fs/operations/copy_test.go b/fs/operations/copy_test.go
index e717f3bef..f62498dab 100644
--- a/fs/operations/copy_test.go
+++ b/fs/operations/copy_test.go
@@ -126,6 +126,40 @@ func TestCopyFile(t *testing.T) {
r.CheckRemoteItems(t, file2)
}
+func TestCopyFileImmutable(t *testing.T) {
+ ctx := context.Background()
+ ctx, ci := fs.AddConfig(ctx)
+ r := fstest.NewRun(t)
+ defer accounting.GlobalStats().ResetCounters()
+
+ ci.Immutable = true
+
+ file1 := r.WriteFile("existing", "potato", t1)
+ r.CheckLocalItems(t, file1)
+
+ err := operations.CopyFile(ctx, r.Fremote, r.Flocal, file1.Path, file1.Path)
+ require.NoError(t, err)
+ r.CheckRemoteItems(t, file1)
+
+ // Copying an unchanged file again is fine
+ err = operations.CopyFile(ctx, r.Fremote, r.Flocal, file1.Path, file1.Path)
+ require.NoError(t, err)
+ r.CheckRemoteItems(t, file1)
+
+ // Should fail with ErrorImmutableModified and not modify local or remote files
+ file2 := r.WriteFile("existing", "tomatoes", t2)
+ r.CheckLocalItems(t, file2)
+ err = operations.CopyFile(ctx, r.Fremote, r.Flocal, file2.Path, file2.Path)
+ assert.ErrorIs(t, err, fs.ErrorImmutableModified)
+ r.CheckLocalItems(t, file2)
+ r.CheckRemoteItems(t, file1)
+
+ ci.NoCheckDest = true
+ err = operations.CopyFile(ctx, r.Fremote, r.Flocal, file2.Path, file2.Path)
+ assert.EqualError(t, err, "can't use --no-check-dest with --immutable")
+ r.CheckRemoteItems(t, file1)
+}
+
// Find the longest file name for writing to local
func maxLengthFileName(t *testing.T, r *fstest.Run) string {
require.NoError(t, r.Flocal.Mkdir(context.Background(), "")) // create the root
diff --git a/fs/operations/operations.go b/fs/operations/operations.go
index 23da01425..eff2c6ae0 100644
--- a/fs/operations/operations.go
+++ b/fs/operations/operations.go
@@ -2090,6 +2090,9 @@ func MoveCaseInsensitive(ctx context.Context, fdst fs.Fs, fsrc fs.Fs, dstFileNam
// moveOrCopyFile moves or copies a single file possibly to a new name
func moveOrCopyFile(ctx context.Context, fdst fs.Fs, fsrc fs.Fs, dstFileName string, srcFileName string, cp bool, allowOverlap bool) (err error) {
ci := fs.GetConfig(ctx)
+ if ci.NoCheckDest && ci.Immutable {
+ return errors.New("can't use --no-check-dest with --immutable")
+ }
logger, usingLogger := GetLogger(ctx)
dstFilePath := path.Join(fdst.Root(), dstFileName)
srcFilePath := path.Join(fsrc.Root(), srcFileName)
@@ -2171,6 +2174,13 @@ func moveOrCopyFile(ctx context.Context, fdst fs.Fs, fsrc fs.Fs, dstFileName str
}
}
if needTransfer {
+ // If files are treated as immutable, fail if destination exists and does not match
+ if ci.Immutable && dstObj != nil {
+ err = fs.CountError(ctx, fserrors.NoRetryError(fs.ErrorImmutableModified))
+ fs.Errorf(dstObj, "Source and destination exist but do not match: %v", err)
+ logger(ctx, TransferError, srcObj, dstObj, err)
+ return err
+ }
// If destination already exists, then we must move it into --backup-dir if required
if dstObj != nil && backupDir != nil {
err = MoveBackupDir(ctx, backupDir, dstObj)
diff --git a/fs/operations/operations_test.go b/fs/operations/operations_test.go
index 39cbfca98..eb9184616 100644
--- a/fs/operations/operations_test.go
+++ b/fs/operations/operations_test.go
@@ -1081,6 +1081,25 @@ func TestMoveFileWithIgnoreExisting(t *testing.T) {
r.CheckRemoteItems(t, file1)
}
+func TestMoveFileImmutable(t *testing.T) {
+ ctx := context.Background()
+ ctx, ci := fs.AddConfig(ctx)
+ r := fstest.NewRun(t)
+ defer accounting.GlobalStats().ResetCounters()
+
+ ci.Immutable = true
+
+ file1 := r.WriteObject(ctx, "existing", "potato", t1)
+ r.CheckRemoteItems(t, file1)
+
+ // Should fail with ErrorImmutableModified and leave the source in place
+ file2 := r.WriteFile("existing", "tomatoes", t2)
+ err := operations.MoveFile(ctx, r.Fremote, r.Flocal, file2.Path, file2.Path)
+ assert.ErrorIs(t, err, fs.ErrorImmutableModified)
+ r.CheckLocalItems(t, file2)
+ r.CheckRemoteItems(t, file1)
+}
+
func TestCaseInsensitiveMoveFile(t *testing.T) {
ctx := context.Background()
r := fstest.NewRun(t)