diff --git a/fs/operations/rc.go b/fs/operations/rc.go index 644a947e9..6ca07ee39 100644 --- a/fs/operations/rc.go +++ b/fs/operations/rc.go @@ -208,7 +208,7 @@ func init() { {name: "rmdir", title: "Remove an empty directory or container"}, {name: "purge", title: "Remove a directory or container and all of its contents"}, {name: "rmdirs", title: "Remove all the empty directories in the path", help: "- leaveRoot - boolean, set to true not to delete the root\n"}, - {name: "delete", title: "Remove files in the path", noRemote: true}, + {name: "delete", title: "Remove files in the path", help: "- rmdirs - boolean, set to true to remove empty directories\n- leaveRoot - boolean if rmdirs is set, set to true not to delete the root\n", noRemote: true}, {name: "deletefile", title: "Remove the single file pointed to"}, {name: "copyurl", title: "Copy the URL to the object", help: "- url - string, URL to read from\n - autoFilename - boolean, set to true to retrieve destination file name from url\n"}, {name: "uploadfile", title: "Upload file using multiform/form-data", help: "- each part in body represents a file to be uploaded\n", needsRequest: true, noCommand: true}, @@ -267,7 +267,22 @@ func rcSingleCommand(ctx context.Context, in rc.Params, name string, noRemote bo } return nil, Rmdirs(ctx, f, remote, leaveRoot) case "delete": - return nil, Delete(ctx, f) + rmdirs, err := in.GetBool("rmdirs") + if rc.NotErrParamNotFound(err) { + return nil, err + } + leaveRoot, err := in.GetBool("leaveRoot") + if rc.NotErrParamNotFound(err) { + return nil, err + } + err = Delete(ctx, f) + if err != nil { + return nil, err + } + if !rmdirs { + return nil, nil + } + return nil, Rmdirs(ctx, f, remote, leaveRoot) case "deletefile": o, err := f.NewObject(ctx, remote) if err != nil { diff --git a/fs/operations/rc_test.go b/fs/operations/rc_test.go index 2d8d0f2af..a44d0d64c 100644 --- a/fs/operations/rc_test.go +++ b/fs/operations/rc_test.go @@ -159,21 +159,32 @@ func TestRcCopyurl(t *testing.T) { // operations/delete: Remove files in the path func TestRcDelete(t *testing.T) { + ctx := context.Background() r, call := rcNewRun(t, "operations/delete") + file1 := r.WriteObject(ctx, "subdir/file1", "subdir/file1 contents", t1) + file2 := r.WriteObject(ctx, "file2", "file2 contents", t1) - file1 := r.WriteObject(context.Background(), "small", "1234567890", t2) // 10 bytes - file2 := r.WriteObject(context.Background(), "medium", "------------------------------------------------------------", t1) // 60 bytes - file3 := r.WriteObject(context.Background(), "large", "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA", t1) // 100 bytes - r.CheckRemoteItems(t, file1, file2, file3) + fstest.CheckListingWithPrecision(t, r.Fremote, []fstest.Item{file1, file2}, []string{"subdir"}, fs.GetModifyWindow(ctx, r.Fremote)) in := rc.Params{ "fs": r.FremoteName, } - out, err := call.Fn(context.Background(), in) + out, err := call.Fn(ctx, in) require.NoError(t, err) assert.Equal(t, rc.Params(nil), out) - r.CheckRemoteItems(t) + fstest.CheckListingWithPrecision(t, r.Fremote, []fstest.Item{}, []string{"subdir"}, fs.GetModifyWindow(ctx, r.Fremote)) + + // Now try with rmdirs=true and leaveRoot=true + in["rmdirs"] = true + in["leaveRoot"] = true + out, err = call.Fn(ctx, in) + require.NoError(t, err) + assert.Equal(t, rc.Params(nil), out) + + fstest.CheckListingWithPrecision(t, r.Fremote, []fstest.Item{}, []string{}, fs.GetModifyWindow(ctx, r.Fremote)) + + // FIXME don't have an easy way of checking the root still exists or not } // operations/deletefile: Remove the single file pointed to