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: respect backup-dir in deletefile

Cao Yuhang36febacabdd8
diff --git a/cmd/deletefile/deletefile.go b/cmd/deletefile/deletefile.go
index 652b39c2e..22e67d03f 100644
--- a/cmd/deletefile/deletefile.go
+++ b/cmd/deletefile/deletefile.go
@@ -4,6 +4,7 @@ package deletefile
import (
"context"
"fmt"
+ "strings"
"github.com/rclone/rclone/cmd"
"github.com/rclone/rclone/fs"
@@ -18,25 +19,37 @@ func init() {
var commandDefinition = &cobra.Command{
Use: "deletefile remote:path",
Short: `Remove a single file from remote.`,
- Long: `Remove a single file from remote. Unlike ` + "`" + `delete` + "`" + ` it cannot be used to
+ // Warning! "|" will be replaced by backticks below
+ Long: strings.ReplaceAll(`Remove a single file from remote. Unlike |delete| it cannot be used to
remove a directory and it doesn't obey include/exclude filters - if the
-specified file exists, it will always be removed.`,
+specified file exists, it will always be removed from its original location.
+
+The |--backup-dir| and |--suffix| flags are honored when set.`, "|", "`"),
Annotations: map[string]string{
"versionIntroduced": "v1.42",
- "groups": "Important",
+ "groups": "Important,Sync",
},
Run: func(command *cobra.Command, args []string) {
cmd.CheckArgs(1, 1, command, args)
f, fileName := cmd.NewFsFile(args[0])
cmd.Run(true, false, command, func() error {
+ ctx := context.Background()
if fileName == "" {
return fmt.Errorf("%s is a directory or doesn't exist: %w", args[0], fs.ErrorObjectNotFound)
}
- fileObj, err := f.NewObject(context.Background(), fileName)
+ fileObj, err := f.NewObject(ctx, fileName)
if err != nil {
return err
}
- return operations.DeleteFile(context.Background(), fileObj)
+ ci := fs.GetConfig(ctx)
+ var backupDir fs.Fs
+ if ci.BackupDir != "" || ci.Suffix != "" {
+ backupDir, err = operations.BackupDir(ctx, f, f, fileName)
+ if err != nil {
+ return err
+ }
+ }
+ return operations.DeleteFileWithBackupDir(ctx, fileObj, backupDir)
})
},
}
diff --git a/fs/operations/rc.go b/fs/operations/rc.go
index de939596d..11b229d6f 100644
--- a/fs/operations/rc.go
+++ b/fs/operations/rc.go
@@ -274,7 +274,15 @@ func rcSingleCommand(ctx context.Context, in rc.Params, name string, noRemote bo
if err != nil {
return nil, err
}
- return nil, DeleteFile(ctx, o)
+ ci := fs.GetConfig(ctx)
+ var backupDir fs.Fs
+ if ci.BackupDir != "" || ci.Suffix != "" {
+ backupDir, err = BackupDir(ctx, f, f, remote)
+ if err != nil {
+ return nil, err
+ }
+ }
+ return nil, DeleteFileWithBackupDir(ctx, o, backupDir)
case "copyurl":
url, err := in.GetString("url")
if err != nil {
diff --git a/fs/operations/rc_test.go b/fs/operations/rc_test.go
index 3cf9c558b..038b4f082 100644
--- a/fs/operations/rc_test.go
+++ b/fs/operations/rc_test.go
@@ -196,6 +196,31 @@ func TestRcDeletefile(t *testing.T) {
r.CheckRemoteItems(t, file2)
}
+func TestRcDeletefileBackupDir(t *testing.T) {
+ ctx := context.Background()
+ ctx, ci := fs.AddConfig(ctx)
+ r, call := rcNewRun(t, "operations/deletefile")
+ if !operations.CanServerSideMove(r.Fremote) {
+ t.Skip("Skipping test as remote does not support server-side move or copy")
+ }
+
+ ci.BackupDir = r.FremoteName + "/backup"
+ file1 := r.WriteObject(ctx, "small", "1234567890", t2)
+ file2 := r.WriteObject(ctx, "medium", strings.Repeat("-", 60), t1)
+ r.CheckRemoteItems(t, file1, file2)
+
+ in := rc.Params{
+ "fs": r.FremoteName,
+ "remote": "small",
+ }
+ out, err := call.Fn(ctx, in)
+ require.NoError(t, err)
+ assert.Equal(t, rc.Params(nil), out)
+
+ file1.Path = "backup/small"
+ r.CheckRemoteItems(t, file1, file2)
+}
+
// operations/list: List the given remote and path in JSON format.
func TestRcList(t *testing.T) {
r, call := rcNewRun(t, "operations/list")