Merge pull request #1295 from krishnasrinivas/prefix-rm

Fix for prefix based rm
This commit is contained in:
Harshavardhana
2015-11-13 15:22:33 -08:00
4 changed files with 39 additions and 17 deletions
+16
View File
@@ -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
}
+1 -1
View File
@@ -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{
+13 -7
View File
@@ -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})
}
+9 -9
View File
@@ -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. */