From eb78783c3f17f2bf9db11d7067f9674b5298ec21 Mon Sep 17 00:00:00 2001 From: Krishna Srinivas Date: Fri, 13 Nov 2015 14:35:45 -0800 Subject: [PATCH] Fix for prefix based rm "mc rm asd..." where asd is a prefix, not a directory --- common-methods.go | 16 ++++++++++++++++ pkg/client/fs/fs.go | 2 +- pkg/client/s3/s3.go | 20 +++++++++++++------- rm-main.go | 18 +++++++++--------- 4 files changed, 39 insertions(+), 17 deletions(-) diff --git a/common-methods.go b/common-methods.go index 7f1d141c..a3ec680a 100644 --- a/common-methods.go +++ b/common-methods.go @@ -196,3 +196,19 @@ func url2Client(url string) (client.Client, *probe.Error) { } return client, nil } + +func prefixExists(url string) bool { + clnt, err := url2Client(url) + if err != nil { + errorIf(err.Trace(url), "Invalid URL ‘"+url+"’.") + } + isRecursive := true + isIncomplete := false + for entry := range clnt.List(isRecursive, isIncomplete) { + if entry.Err != nil { + return false + } + return true + } + return false +} diff --git a/pkg/client/fs/fs.go b/pkg/client/fs/fs.go index abe37568..c71c7725 100644 --- a/pkg/client/fs/fs.go +++ b/pkg/client/fs/fs.go @@ -242,7 +242,7 @@ func (f *fsClient) listInRoutine(contentCh chan client.ContentOnChannel) { fst, err := f.fsStat() if err != nil { - if os.IsNotExist(err.ToGoError()) { + if _, ok := err.ToGoError().(client.PathNotFound); ok { dir, err := os.Open(filepath.Dir(fpath)) if err != nil { contentCh <- client.ContentOnChannel{ diff --git a/pkg/client/s3/s3.go b/pkg/client/s3/s3.go index 29d35aa3..cd245b45 100644 --- a/pkg/client/s3/s3.go +++ b/pkg/client/s3/s3.go @@ -230,14 +230,20 @@ func (c *s3Client) Stat() (*client.Content, *probe.Error) { errResponse := minio.ToErrorResponse(err) if errResponse != nil { if errResponse.Code == "NoSuchKey" { - for content := range c.List(false, false) { - if content.Err != nil { - return nil, content.Err.Trace() + // append "/" to the object name and see if we can list anything + // if so, we consider it as a directory + dirName := object + if !strings.HasSuffix(dirName, string(c.hostURL.Separator)) { + dirName += string(c.hostURL.Separator) + } + for objectStat := range c.api.ListObjects(bucket, dirName, false) { + if objectStat.Err != nil { + return nil, probe.NewError(objectStat.Err) } - content.Content.URL = *c.hostURL - content.Content.Type = os.ModeDir - content.Content.Size = 0 - return content.Content, nil + content := client.Content{} + content.URL = *c.hostURL + content.Type = os.ModeDir + return &content, nil } return nil, probe.NewError(client.PathNotFound{Path: c.hostURL.Path}) } diff --git a/rm-main.go b/rm-main.go index a444a8d4..e1e979d1 100644 --- a/rm-main.go +++ b/rm-main.go @@ -65,22 +65,22 @@ EXAMPLES: $ mc {{.Name}} 1999/old-backup.tgz 3. Remove contents of a folder recursively. - $ mc {{.Name}} --force https://s3.amazonaws.com/jazz-songs/louis/... + $ mc {{.Name}} --force https://s3.amazonaws.com/jazz-songs/louis/... 4 Remove contents of a folder recursively and folder itself. $ mc {{.Name}} old/photos... 5. Remove a bucket and all its contents recursively. - $ mc {{.Name}} --force https://s3.amazonaws.com/jazz-songs... + $ 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... + $ mc {{.Name}} --force https://s3.amazonaws.com/ogg/gunmetal... 7. Cancel an incomplete upload of an object. - $ mc {{.Name}} --incomplete https://s3.amazonaws.com/jazz-songs/louis/file01.mp3 + $ 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/... + $ mc {{.Name}} --incomplete --force https://s3.amazonaws.com/jazz-songs/louis/... `, } @@ -155,7 +155,6 @@ func rmAll(url string, isIncomplete bool) { 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) { if entry.Err != nil { @@ -197,12 +196,13 @@ func mainRm(ctx *cli.Context) { if isURLRecursive(url) && isForce { url := stripRecursiveURL(url) removeTopFolder := false - // find if the URL is dir or not. _, content, err := url2Stat(url) - fatalIf(err.Trace(url), "Unable to stat ‘"+url+"’.") + if err != nil && !prefixExists(url) { + fatalIf(err.Trace(url), "Unable to stat ‘"+url+"’.") + } - if content.Type.IsDir() { + 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. */