Merge pull request #1443 from krishnasrinivas/rm-url2dircontent

mc ls fails when there is a broken symlink - fixes #1442
This commit is contained in:
Harshavardhana
2015-11-19 12:48:16 -08:00
4 changed files with 11 additions and 70 deletions
+10 -32
View File
@@ -103,38 +103,16 @@ func parseContent(c *client.Content) contentMessage {
return content
}
// trimContent to fancify the output for directories.
func trimContent(parentContentDir, childContent *client.Content) *client.Content {
// If parentContentDir is a directory, use it to trim the sub-folders.
if parentContentDir.Type.IsDir() {
// Allocate a new client.Content for trimmed output.
trimmedContent := new(client.Content)
trimmedContent = childContent
if parentContentDir.URL.Path == string(parentContentDir.URL.Separator) {
trimmedContent.URL.Path = strings.TrimPrefix(trimmedContent.URL.Path, parentContentDir.URL.Path)
return trimmedContent
}
// If the beginning of the trimPrefix is a URL.Separator ignore it.
trimmedContent.URL.Path = strings.TrimPrefix(trimmedContent.URL.Path, string(trimmedContent.URL.Separator))
trimPrefixContentPath := parentContentDir.URL.Path[:strings.LastIndex(parentContentDir.URL.Path,
string(parentContentDir.URL.Separator))+1]
// If the beginning of the trimPrefix is a URL.Separator ignore it.
trimPrefixContentPath = strings.TrimPrefix(trimPrefixContentPath, string(parentContentDir.URL.Separator))
trimmedContent.URL.Path = strings.TrimPrefix(trimmedContent.URL.Path, trimPrefixContentPath)
return trimmedContent
}
// if the target is a file, no more trimming needed return back as is.
return childContent
}
// doList - list all entities inside a folder.
func doList(clnt client.Client, isRecursive, isIncomplete bool) *probe.Error {
// parentContentDir is verified prefix of clnt URL used for trimming purposes.
parentContentDir, err := url2DirContent(clnt.GetURL().String())
if err != nil {
return err.Trace(clnt.GetURL().String())
prefixPath := clnt.GetURL().Path
separator := string(clnt.GetURL().Separator)
if !strings.HasSuffix(prefixPath, separator) {
prefixPath = prefixPath[:strings.LastIndex(prefixPath, separator)+1]
}
for content := range clnt.List(isRecursive, isIncomplete) {
// fmt.Println(content)
if content.Err != nil {
switch content.Err.ToGoError().(type) {
// handle this specifically for filesystem related errors.
@@ -154,10 +132,10 @@ func doList(clnt client.Client, isRecursive, isIncomplete bool) *probe.Error {
errorIf(content.Err.Trace(), "Unable to list folder.")
continue
}
// trim incoming content based on if its recursive or not.
trimmedContent := trimContent(parentContentDir, content)
// parse trimmed content into printable form.
parsedContent := parseContent(trimmedContent)
contentURL := content.URL.Path
contentURL = strings.TrimPrefix(contentURL, prefixPath)
content.URL.Path = contentURL
parsedContent := parseContent(content)
// print colorized or jsonized content info.
printMsg(parsedContent)
}
+1 -7
View File
@@ -311,13 +311,7 @@ func (f *fsClient) listInRoutine(contentCh chan<- *client.Content) {
// Now if the file exists and doesn't end with a separator ('/') do not traverse it.
// If the directory doesn't end with a separator, do not traverse it.
if !strings.HasSuffix(fpath, string(pathURL.Separator)) && fst.Mode().IsDir() && fpath != "." {
contentCh <- &client.Content{
URL: pathURL,
Time: fst.ModTime(),
Size: fst.Size(),
Type: fst.Mode(),
Err: nil,
}
f.listPrefixes(fpath, contentCh)
return
}
-26
View File
@@ -17,7 +17,6 @@
package main
import (
"os"
"strings"
"github.com/minio/mc/pkg/client"
@@ -93,31 +92,6 @@ func url2Stat(urlStr string) (client client.Client, content *client.Content, err
return client, content, nil
}
// url2DirContent returns directory content info for a URL.
func url2DirContent(urlStr string) (content *client.Content, err *probe.Error) {
clnt, err := url2Client(urlStr)
if err != nil {
return nil, err.Trace(urlStr)
}
isRecursive := false
isIncomplete := false
for entry := range clnt.List(isRecursive, isIncomplete) {
if entry.Err != nil {
return nil, entry.Err.Trace(urlStr)
}
if strings.HasPrefix(entry.URL.Path, clnt.GetURL().Path) {
content := new(client.Content)
content.URL = clnt.GetURL()
content.Type = os.ModeDir
return content, nil
}
}
content = new(client.Content)
content.URL = clnt.GetURL()
content.Type = os.ModeDir
return content, nil
}
// Check if object key prefix exists.
func prefixExists(urlStr string) bool {
clnt, err := url2Client(urlStr)
-5
View File
@@ -37,8 +37,3 @@ func (s *TestSuite) TestURLJoinPath(c *C) {
url = urlJoinPath(url1, url2)
c.Assert(url, Equals, "http://s3.mycompany.io/dev/mybucket/bin/")
}
func (s *TestSuite) TestURL2DirContent(c *C) {
_, err := url2DirContent(".")
c.Assert(err, IsNil)
}