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.
This commit is contained in:
Anis Elleuch
2019-03-08 17:07:06 +05:30
committed by Nitish Tiwari
parent b72d2d9cc5
commit 4a130c60a8
2 changed files with 33 additions and 11 deletions
+8 -1
View File
@@ -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)
}
+25 -10
View File
@@ -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)
}
}