From 4a130c60a8ab5508abe079a23e24fdfef99c610f Mon Sep 17 00:00:00 2001 From: Anis Elleuch Date: Fri, 8 Mar 2019 12:37:06 +0100 Subject: [PATCH] rb: Fix removing an empty bucket when passed as argument (#2703) rb was relying on client.List() to find all elements that needs to be removed. However it doesn't include 'alias/bucket' in the listing when 'alias/bucket' itself is passed as an argument in rb command. This PR will always attempt to delete the passed argument as this is what the user wants in the first place. This PR also fixes teardown() in functional-tests.sh to remove the bucket using rb instead rm since rm does not remove buckets anymore. --- cmd/client-s3.go | 9 ++++++++- cmd/rb-main.go | 35 +++++++++++++++++++++++++---------- 2 files changed, 33 insertions(+), 11 deletions(-) diff --git a/cmd/client-s3.go b/cmd/client-s3.go index 93f1a174..cbae3d0a 100644 --- a/cmd/client-s3.go +++ b/cmd/client-s3.go @@ -792,6 +792,13 @@ func (c *s3Client) Remove(isIncomplete, isRemoveBucket bool, contentCh <-chan *c for content := range contentCh { // Convert content.URL.Path to objectName for objectsCh. bucket, objectName := c.splitPath(content.URL.Path) + + // We don't treat path when bucket is + // empty, just skip it when it happens. + if bucket == "" { + continue + } + // Init objectsCh the first time. if prevBucket == "" { objectsCh = make(chan string) @@ -858,7 +865,7 @@ func (c *s3Client) Remove(isIncomplete, isRemoveBucket bool, contentCh <-chan *c } } // Remove last bucket if it qualifies. - if isRemoveBucket && !isIncomplete { + if isRemoveBucket && prevBucket != "" && !isIncomplete { if err := c.api.RemoveBucket(prevBucket); err != nil { errorCh <- probe.NewError(err) } diff --git a/cmd/rb-main.go b/cmd/rb-main.go index 3ed93f47..febeabc1 100644 --- a/cmd/rb-main.go +++ b/cmd/rb-main.go @@ -94,12 +94,11 @@ func checkRbSyntax(ctx *cli.Context) { } // deletes a bucket and all its contents -func deleteBucket(url string) error { +func deleteBucket(url string) *probe.Error { targetAlias, targetURL, _ := mustExpandAlias(url) clnt, pErr := newClientFromAlias(targetAlias, targetURL) if pErr != nil { - errorIf(pErr.Trace(url), "Failed to remove `"+url+"`.") - return exitStatus(globalErrorExitStatus) // End of journey. + return pErr } var isIncomplete bool isRemoveBucket := true @@ -108,14 +107,14 @@ func deleteBucket(url string) error { for content := range clnt.List(true, false, DirLast) { if content.Err != nil { - errorIf(content.Err.Trace(url), "Failed to remove `"+url+"`.") switch content.Err.ToGoError().(type) { case PathInsufficientPermission: + errorIf(content.Err.Trace(url), "Failed to remove `"+url+"`.") // Ignore Permission error. continue } close(contentCh) - return exitStatus(globalErrorExitStatus) + return content.Err } urlString := content.URL.Path @@ -125,27 +124,31 @@ func deleteBucket(url string) error { case contentCh <- content: sent = true case pErr := <-errorCh: - errorIf(pErr.Trace(urlString), "Failed to remove `"+urlString+"`.") switch pErr.ToGoError().(type) { case PathInsufficientPermission: + errorIf(pErr.Trace(urlString), "Failed to remove `"+urlString+"`.") // Ignore Permission error. continue } close(contentCh) - return exitStatus(globalErrorExitStatus) + return pErr } } } + // Remove the given url since the user will always want to remove it. + contentCh <- &clientContent{URL: *newClientURL(targetURL)} + + // Finish removing and print all the remaining errors close(contentCh) for pErr := range errorCh { - errorIf(pErr.Trace(url), "Failed to remove `"+url+"`.") switch pErr.ToGoError().(type) { case PathInsufficientPermission: + errorIf(pErr.Trace(url), "Failed to remove `"+url+"`.") // Ignore Permission error. continue } - return exitStatus(globalErrorExitStatus) + return pErr } return nil } @@ -182,6 +185,17 @@ func mainRemoveBucket(ctx *cli.Context) error { cErr = exitStatus(globalErrorExitStatus) continue } + _, err = clnt.Stat(false, false, nil) + if err != nil { + switch err.ToGoError().(type) { + case BucketNameEmpty: + default: + errorIf(err.Trace(targetURL), "Unable to validate target `"+targetURL+"`.") + cErr = exitStatus(globalErrorExitStatus) + continue + + } + } isEmpty := true for range clnt.List(true, false, DirNone) { isEmpty = false @@ -202,7 +216,8 @@ func mainRemoveBucket(ctx *cli.Context) error { // Successfully removed a bucket. printMsg(removeBucketMessage{Status: "success", Bucket: targetURL}) } else { - errorIf(probe.NewError(e), `"+targetURL+"`+"could not be removed.") + errorIf(e.Trace(targetURL), "Failed to remove `"+targetURL+"`.") + cErr = exitStatus(globalErrorExitStatus) } }