From 89930fed29e78a558400208256a76a6446dfa9d5 Mon Sep 17 00:00:00 2001 From: Harshavardhana Date: Sun, 19 Apr 2020 09:50:18 -0700 Subject: [PATCH] rb shouldn't hang on empty folders (#3178) fixes #3176 fixes #3177 --- cmd/client-fs.go | 29 ++++++++++++------- cmd/rb-main.go | 75 +++++++++++++++++++++--------------------------- cmd/rm-main.go | 13 +++++++-- 3 files changed, 62 insertions(+), 55 deletions(-) diff --git a/cmd/client-fs.go b/cmd/client-fs.go index 5361c44f..d7efd378 100644 --- a/cmd/client-fs.go +++ b/cmd/client-fs.go @@ -512,22 +512,29 @@ func (f *fsClient) Remove(isIncomplete, isRemoveBucket, isBypass bool, contentCh defer close(errorCh) for content := range contentCh { + if content.Err != nil { + errorCh <- content.Err + continue + } name := content.URL.Path // Add partSuffix for incomplete uploads. if isIncomplete { name += partSuffix } - if e := deleteFile(name); e != nil { - if os.IsPermission(e) { - // Ignore permission error. - errorCh <- probe.NewError(PathInsufficientPermission{Path: content.URL.Path}) - } else if os.IsNotExist(e) && isRemoveBucket { - // ignore PathNotFound for dir removal. - return - } else { - errorCh <- probe.NewError(e) - return - } + e := deleteFile(name) + if e == nil { + continue + } + if os.IsNotExist(e) && isRemoveBucket { + // ignore PathNotFound for dir removal. + return + } + if os.IsPermission(e) { + // Ignore permission error. + errorCh <- probe.NewError(PathInsufficientPermission{Path: content.URL.Path}) + } else { + errorCh <- probe.NewError(e) + return } } }() diff --git a/cmd/rb-main.go b/cmd/rb-main.go index 12e13651..9934db40 100644 --- a/cmd/rb-main.go +++ b/cmd/rb-main.go @@ -17,6 +17,7 @@ package cmd import ( + "context" "fmt" "path/filepath" "strings" @@ -114,6 +115,9 @@ func checkRbSyntax(ctx *cli.Context) { // deletes a bucket and all its contents func deleteBucket(url string) *probe.Error { + ctx, cancelCopy := context.WithCancel(globalContext) + defer cancelCopy() + targetAlias, targetURL, _ := mustExpandAlias(url) clnt, pErr := newClientFromAlias(targetAlias, targetURL) if pErr != nil { @@ -124,61 +128,47 @@ func deleteBucket(url string) *probe.Error { contentCh := make(chan *ClientContent) errorCh := clnt.Remove(isIncomplete, isRemoveBucket, false, contentCh) - for content := range clnt.List(true, false, false, DirLast) { - if content.Err != nil { - switch content.Err.ToGoError().(type) { - case PathInsufficientPermission: - errorIf(content.Err.Trace(url), "Failed to remove `"+url+"`.") - // Ignore Permission error. + go func() { + defer close(contentCh) + for content := range clnt.List(true, false, false, DirLast) { + if content.Err != nil { + contentCh <- content continue } - close(contentCh) - return content.Err - } - urlString := content.URL.Path - sent := false - for !sent { + urlString := content.URL.Path + select { case contentCh <- content: - sent = true - case pErr := <-errorCh: - switch pErr.ToGoError().(type) { - case PathInsufficientPermission: - errorIf(pErr.Trace(urlString), "Failed to remove `"+urlString+"`.") - // Ignore Permission error. - continue - } - close(contentCh) - return pErr + case <-ctx.Done(): + return + } + + // list internally mimics recursive directory listing of object prefixes for s3 similar to FS. + // The rmMessage needs to be printed only for actual buckets being deleted and not objects. + tgt := strings.TrimPrefix(urlString, string(filepath.Separator)) + if !strings.Contains(tgt, string(filepath.Separator)) && tgt != targetAlias { + printMsg(removeBucketMessage{ + Bucket: targetAlias + urlString, Status: "success", + }) } } - // list internally mimics recursive directory listing of object prefixes for s3 similar to FS. - // The rmMessage needs to be printed only for actual buckets being deleted and not objects. - tgt := strings.TrimPrefix(urlString, string(filepath.Separator)) - if !strings.Contains(tgt, string(filepath.Separator)) && tgt != targetAlias { - printMsg(removeBucketMessage{ - Bucket: targetAlias + urlString, Status: "success", - }) + + // Remove the given url since the user will always want to remove it. + alias, _ := url2Alias(targetURL) + if alias != "" { + contentCh <- &ClientContent{URL: *newClientURL(targetURL)} } - } + }() - // Remove the given url since the user will always want to remove it. - alias, _ := url2Alias(targetURL) - if alias != "" { - contentCh <- &ClientContent{URL: *newClientURL(targetURL)} - } - - // Finish removing and print all the remaining errors - close(contentCh) - for pErr := range errorCh { - switch pErr.ToGoError().(type) { + for perr := range errorCh { + switch perr.ToGoError().(type) { case PathInsufficientPermission: - errorIf(pErr.Trace(url), "Failed to remove `"+url+"`.") + errorIf(perr.Trace(url), "Failed to remove `"+url+"`.") // Ignore Permission error. continue } - return pErr + return perr } return nil } @@ -226,6 +216,7 @@ func mainRemoveBucket(ctx *cli.Context) error { } } + isEmpty := true for range clnt.List(true, false, false, DirNone) { isEmpty = false diff --git a/cmd/rm-main.go b/cmd/rm-main.go index ed44979d..99fa2122 100644 --- a/cmd/rm-main.go +++ b/cmd/rm-main.go @@ -158,14 +158,23 @@ func checkRmSyntax(ctx *cli.Context, encKeyDB map[string][]prefixSSEPair) { for _, url := range ctx.Args() { // clean path for aliases like s3/. - //Note: UNC path using / works properly in go 1.9.2 even though it breaks the UNC specification. + // Note: UNC path using / works properly in go 1.9.2 even though it breaks the UNC specification. url = filepath.ToSlash(filepath.Clean(url)) // namespace removal applies only for non FS. So filter out if passed url represents a directory - if !isAliasURLDir(url, encKeyDB) { + dir := isAliasURLDir(url, encKeyDB) + if !dir { _, path := url2Alias(url) isNamespaceRemoval = (path == "") break } + if dir && isRecursive && !isForce { + fatalIf(errDummy().Trace(), + "Removal requires --force flag. This operation is *IRREVERSIBLE*. Please review carefully before performing this *DANGEROUS* operation.") + } + if dir && !isRecursive { + fatalIf(errDummy().Trace(), + "Removal requires --recursive flag. This operation is *IRREVERSIBLE*. Please review carefully before performing this *DANGEROUS* operation.") + } } if !ctx.Args().Present() && !isStdin { exitCode := 1