diff --git a/rm-main.go b/rm-main.go index b41cc24c..2812a0f3 100644 --- a/rm-main.go +++ b/rm-main.go @@ -23,23 +23,24 @@ import ( "github.com/fatih/color" "github.com/minio/cli" - "github.com/minio/mc/pkg/client" "github.com/minio/mc/pkg/console" "github.com/minio/minio-xl/pkg/probe" ) // rm specific flags. var ( + rmFlagRecursive = cli.BoolFlag{ + Name: "recursive, r", + Usage: "Remove recursively.", + } rmFlagForce = cli.BoolFlag{ Name: "force", Usage: "Force a dangerous remove operation.", } - rmFlagIncomplete = cli.BoolFlag{ Name: "incomplete, I", Usage: "Remove an incomplete upload(s).", } - rmFlagHelp = cli.BoolFlag{ Name: "help, h", Usage: "Help of rm.", @@ -51,7 +52,7 @@ var rmCmd = cli.Command{ Name: "rm", Usage: "Remove file or bucket [WARNING: Use with care].", Action: mainRm, - Flags: []cli.Flag{rmFlagForce, rmFlagIncomplete, rmFlagHelp}, + Flags: []cli.Flag{rmFlagRecursive, rmFlagForce, rmFlagIncomplete, rmFlagHelp}, CustomHelpTemplate: `NAME: mc {{.Name}} - {{.Usage}} @@ -62,29 +63,23 @@ FLAGS: {{range .Flags}}{{.}} {{end}} EXAMPLES: - 1. Remove an object. - $ mc {{.Name}} https://s3.amazonaws.com/jazz-songs/louis/file01.mp3 - - 2. Remove a file. + 1. Remove a file. $ mc {{.Name}} 1999/old-backup.tgz + 2. Remove contents of a folder, excluding its sub-folders. + $ mc {{.Name}} --force https://s3.amazonaws.com/jazz-songs/louis/ + 3. Remove contents of a folder recursively. - $ mc {{.Name}} --force https://s3.amazonaws.com/jazz-songs/louis/... + $ mc {{.Name}} --force --recursive https://s3.amazonaws.com/jazz-songs/louis/ - 4 Remove contents of a folder recursively and folder itself. - $ mc {{.Name}} old/photos... + 4. Remove all matching objects with this prefix. + $ mc {{.Name}} --force --force https://s3.amazonaws.com/ogg/gunmetal - 5. Remove a bucket and all its contents recursively. - $ mc {{.Name}} --force https://s3.amazonaws.com/jazz-songs... - - 6. Remove all matching objects with this prefix. - $ mc {{.Name}} --force https://s3.amazonaws.com/ogg/gunmetal... - - 7. Cancel an incomplete upload of an object. + 5. Drop an incomplete upload of an object. $ mc {{.Name}} --incomplete https://s3.amazonaws.com/jazz-songs/louis/file01.mp3 - 8. Remove all incomplete uploads recursively matching this prefix. - $ mc {{.Name}} --incomplete --force https://s3.amazonaws.com/jazz-songs/louis/... + 6. Drop all incomplete uploads recursively matching this prefix. + $ mc {{.Name}} --incomplete --force --recursive https://s3.amazonaws.com/jazz-songs/louis/ `, } @@ -107,30 +102,17 @@ func (r rmMessage) JSON() string { // Validate command line arguments. func checkRmSyntax(ctx *cli.Context) { - args := ctx.Args() isForce := ctx.Bool("force") + isRecursive := ctx.Bool("recursive") - if !args.Present() { + if !ctx.Args().Present() { exitCode := 1 cli.ShowCommandHelpAndExit(ctx, "rm", exitCode) } - URLs, err := args2URLs(args) - fatalIf(err.Trace(ctx.Args()...), "Unable to parse arguments.") - - // If input validation fails then provide context sensitive help without displaying generic help message. - // The context sensitive help is shown per argument instead of all arguments to keep the help display - // as well as the code simple. Also most of the times there will be just one arg - for _, url := range URLs { - u := client.NewURL(url) - if strings.HasSuffix(url, string(u.Separator)) { - fatalIf(errDummy().Trace(), - "‘"+url+"’ is a folder. To remove this folder recursively, please try ‘"+url+"...’ as argument.") - } - if isURLRecursive(url) && !isForce { - fatalIf(errDummy().Trace(), - "Recursive removal requires --force option. Please review carefully before performing this operation.") - } + if isRecursive && !isForce { + fatalIf(errDummy().Trace(), + "Recursive removal requires --force option. Please review carefully before performing this *DANGEROUS* operation.") } } @@ -148,31 +130,34 @@ func rm(url string, isIncomplete bool) *probe.Error { } // Remove all objects recursively. -func rmAll(url string, isIncomplete bool) { +func rmAll(url string, isRecursive, isIncomplete bool) { // Initialize new client. clnt, err := url2Client(url) if err != nil { errorIf(err.Trace(url), "Invalid URL ‘"+url+"’.") return // End of journey. } - isRecursive := false // Disable recursion and only list this folder's contents. - for entry := range clnt.List(isRecursive, isIncomplete) { + + /* Disable recursion and only list this folder's contents. We + perform manual depth-first recursion ourself here. */ + nonRecursive := false + for entry := range clnt.List(nonRecursive, isIncomplete) { if entry.Err != nil { errorIf(entry.Err.Trace(url), "Unable to list ‘"+url+"’.") return // End of journey. } - if entry.Type.IsDir() { + if entry.Type.IsDir() && isRecursive { // Add separator at the end to remove all its contents. url := entry.URL url.Path = strings.TrimSuffix(entry.URL.Path, string(entry.URL.Separator)) + string(entry.URL.Separator) // Recursively remove contents of this directory. - rmAll(url.String(), isIncomplete) + rmAll(url.String(), isRecursive, isIncomplete) } + // Regular type. - err = rm(entry.URL.String(), isIncomplete) - if err != nil { + if err = rm(entry.URL.String(), isIncomplete); err != nil { errorIf(err.Trace(entry.URL.String()), "Unable to remove ‘"+entry.URL.String()+"’.") continue } @@ -187,6 +172,7 @@ func mainRm(ctx *cli.Context) { // rm specific flags. isForce := ctx.Bool("force") isIncomplete := ctx.Bool("incomplete") + isRecursive := ctx.Bool("recursive") // Set color. console.SetColor("Remove", color.New(color.FgGreen, color.Bold)) @@ -197,45 +183,11 @@ func mainRm(ctx *cli.Context) { // Support multiple targets. for _, url := range URLs { - if isURLRecursive(url) && isForce { - url := stripRecursiveURL(url) - removeTopFolder := false - // find if the URL is dir or not. - _, content, err := url2Stat(url) - if err != nil && !prefixExists(url) { - fatalIf(err.Trace(url), "Unable to stat ‘"+url+"’.") - } - - if err == nil && content.Type.IsDir() { - // Determine whether to remove the top folder or only its - // contents. If the URL does not end with a separator, then - // include the top folder as well, otherwise not. - u := client.NewURL(url) - if !strings.HasSuffix(url, string(u.Separator)) { - // Add separator at the end to remove all its contents. - url = url + string(u.Separator) - // Remember to remove the top most folder. - removeTopFolder = true - } - } - // Remove contents of this folder. - rmAll(url, isIncomplete) - // Ignore url's './', '../' . - if removeTopFolder && url != "./" && url != "../" { - // Remove top folder as well. - err := rm(url, isIncomplete) - if err != nil { - errorIf(err.Trace(url), "Unable to remove ‘"+url+"’.") - continue - } - printMsg(rmMessage{url}) - } - + if isRecursive && isForce { + rmAll(url, isRecursive, isIncomplete) } else { - err := rm(url, isIncomplete) - if err != nil { + if err := rm(url, isIncomplete); err != nil { errorIf(err.Trace(url), "Unable to remove ‘"+url+"’.") - continue } printMsg(rmMessage{url}) } diff --git a/rm_test.go b/rm_test.go index a466f9c9..f1bcfc0f 100644 --- a/rm_test.go +++ b/rm_test.go @@ -60,14 +60,14 @@ func (s *TestSuite) TestRemove(c *C) { // reset back console.IsExited = false - e = app.Run([]string{os.Args[0], "rm", filepath.Join(root, "...")}) + e = app.Run([]string{os.Args[0], "rm", "--recursive", root}) c.Assert(e, IsNil) c.Assert(console.IsExited, Equals, true) // reset back console.IsExited = false - e = app.Run([]string{os.Args[0], "rm", "--force", filepath.Join(root, "...")}) + app.Run([]string{os.Args[0], "rm", "--recursive", "--force", root}) c.Assert(e, IsNil) c.Assert(console.IsExited, Equals, false)