Merge pull request #558 from harshavardhana/pr_out_non_recursive_ls_fixes_479
This commit is contained in:
@@ -19,6 +19,7 @@ package main
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/dustin/go-humanize"
|
||||
@@ -39,13 +40,32 @@ func printItem(date time.Time, v int64, name string, fileType os.FileMode) {
|
||||
fmt.Printf(console.Size("%6s ", humanize.IBytes(uint64(v))))
|
||||
// just making it explicit
|
||||
if fileType.IsDir() {
|
||||
fmt.Println(console.Dir("%s/", name))
|
||||
if strings.HasSuffix(name, "/") {
|
||||
fmt.Println(console.Dir("%s", name))
|
||||
} else {
|
||||
fmt.Println(console.Dir("%s/", name))
|
||||
}
|
||||
}
|
||||
if fileType.IsRegular() {
|
||||
fmt.Println(console.File("%s", name))
|
||||
}
|
||||
}
|
||||
|
||||
func doListSingle(clnt client.Client, targetURL string) error {
|
||||
var err error
|
||||
for itemCh := range clnt.ListSingle() {
|
||||
if itemCh.Err != nil {
|
||||
err = itemCh.Err
|
||||
break
|
||||
}
|
||||
printItem(itemCh.Item.Time, itemCh.Item.Size, itemCh.Item.Name, itemCh.Item.FileType)
|
||||
}
|
||||
if err != nil {
|
||||
return iodine.New(err, map[string]string{"Target": targetURL})
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func doList(clnt client.Client, targetURL string) error {
|
||||
var err error
|
||||
for itemCh := range clnt.List() {
|
||||
@@ -93,15 +113,45 @@ func runListCmd(ctx *cli.Context) {
|
||||
targetURLConfigMap[targetURL] = targetConfig
|
||||
}
|
||||
for targetURL, targetConfig := range targetURLConfigMap {
|
||||
err = doListCmd(mcClientMethods{}, targetURL, targetConfig, globalDebugFlag)
|
||||
err = iodine.New(err, nil)
|
||||
if err != nil {
|
||||
log.Debug.Println(err)
|
||||
console.Fatalf("Failed to list [%s]. Reason: [%s].\n", targetURL, iodine.ToError(err))
|
||||
if isURLRecursive(targetURL) {
|
||||
// if recursive strip off the "..."
|
||||
targetURL = strings.TrimSuffix(targetURL, recursiveSeparator)
|
||||
err = doListCmd(mcClientMethods{}, targetURL, targetConfig, globalDebugFlag)
|
||||
err = iodine.New(err, nil)
|
||||
if err != nil {
|
||||
log.Debug.Println(err)
|
||||
console.Fatalf("Failed to list [%s]. Reason: [%s].\n", targetURL, iodine.ToError(err))
|
||||
}
|
||||
} else {
|
||||
err = doListSingleCmd(mcClientMethods{}, targetURL, targetConfig, globalDebugFlag)
|
||||
if err != nil {
|
||||
if err != nil {
|
||||
log.Debug.Println(err)
|
||||
console.Fatalf("Failed to list [%s]. Reason: [%s].\n", targetURL, iodine.ToError(err))
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func doListSingleCmd(methods clientMethods, targetURL string, targetConfig *hostConfig, debug bool) error {
|
||||
clnt, err := methods.getNewClient(targetURL, targetConfig, globalDebugFlag)
|
||||
if err != nil {
|
||||
return iodine.New(err, map[string]string{"Target": targetURL})
|
||||
}
|
||||
err = doListSingle(clnt, targetURL)
|
||||
for i := 0; i < globalMaxRetryFlag && err != nil && isValidRetry(err); i++ {
|
||||
fmt.Println(console.Retry("Retrying ... %d", i))
|
||||
// Progressively longer delays
|
||||
time.Sleep(time.Duration(i*i) * time.Second)
|
||||
err = doListSingle(clnt, targetURL)
|
||||
}
|
||||
if err != nil {
|
||||
return iodine.New(err, nil)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func doListCmd(methods clientMethods, targetURL string, targetConfig *hostConfig, debug bool) error {
|
||||
clnt, err := methods.getNewClient(targetURL, targetConfig, globalDebugFlag)
|
||||
if err != nil {
|
||||
|
||||
+2
-2
@@ -267,7 +267,7 @@ func (s *CmdTestSuite) TestLsCmdWithBucket(c *C) {
|
||||
methods := &MockclientMethods{}
|
||||
cl1 := &clientMocks.Client{}
|
||||
|
||||
data1 := "hello1"
|
||||
data1 := "hello world 1"
|
||||
dataLen1 := int64(len(data1))
|
||||
|
||||
data2 := "hello world 2"
|
||||
@@ -310,7 +310,7 @@ func (s *CmdTestSuite) TestLsCmdWithFilePath(c *C) {
|
||||
methods := &MockclientMethods{}
|
||||
cl1 := &clientMocks.Client{}
|
||||
|
||||
data1 := "hello1"
|
||||
data1 := "hello world 1"
|
||||
dataLen1 := int64(len(data1))
|
||||
|
||||
data2 := "hello world 2"
|
||||
|
||||
@@ -30,6 +30,7 @@ type Client interface {
|
||||
|
||||
// Common operations
|
||||
Stat() (item *Item, err error)
|
||||
ListSingle() <-chan ItemOnChannel
|
||||
List() <-chan ItemOnChannel
|
||||
|
||||
// Bucket operations
|
||||
|
||||
@@ -123,6 +123,43 @@ func (f *fsClient) GetPartial(offset, length int64) (io.ReadCloser, int64, strin
|
||||
return body, length, md5Str, nil
|
||||
}
|
||||
|
||||
func (f *fsClient) ListSingle() <-chan client.ItemOnChannel {
|
||||
itemCh := make(chan client.ItemOnChannel)
|
||||
go f.listSingle(itemCh)
|
||||
return itemCh
|
||||
}
|
||||
|
||||
func (f *fsClient) listSingle(itemCh chan client.ItemOnChannel) {
|
||||
defer close(itemCh)
|
||||
dir, err := os.Open(f.path)
|
||||
if err != nil {
|
||||
itemCh <- client.ItemOnChannel{
|
||||
Item: nil,
|
||||
Err: iodine.New(err, nil),
|
||||
}
|
||||
}
|
||||
files, err := dir.Readdir(-1)
|
||||
if err != nil {
|
||||
itemCh <- client.ItemOnChannel{
|
||||
Item: nil,
|
||||
Err: iodine.New(err, nil),
|
||||
}
|
||||
}
|
||||
dir.Close()
|
||||
for _, file := range files {
|
||||
item := &client.Item{
|
||||
Name: file.Name(),
|
||||
Time: file.ModTime(),
|
||||
Size: file.Size(),
|
||||
FileType: file.Mode(),
|
||||
}
|
||||
itemCh <- client.ItemOnChannel{
|
||||
Item: item,
|
||||
Err: nil,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func (f *fsClient) List() <-chan client.ItemOnChannel {
|
||||
itemCh := make(chan client.ItemOnChannel)
|
||||
go f.listInGoroutine(itemCh)
|
||||
|
||||
@@ -51,6 +51,13 @@ func (m *Client) Stat() (*client.Item, error) {
|
||||
return r0, r1
|
||||
}
|
||||
|
||||
// ListSingle is a mock method
|
||||
func (m *Client) ListSingle() <-chan client.ItemOnChannel {
|
||||
ret := m.Called()
|
||||
r0 := ret.Get(0).(chan client.ItemOnChannel)
|
||||
return r0
|
||||
}
|
||||
|
||||
// List is a mock method
|
||||
func (m *Client) List() <-chan client.ItemOnChannel {
|
||||
ret := m.Called()
|
||||
|
||||
@@ -24,6 +24,8 @@ import (
|
||||
"io"
|
||||
"net/http"
|
||||
"net/url"
|
||||
"os"
|
||||
"time"
|
||||
|
||||
"github.com/minio-io/mc/pkg/client"
|
||||
"github.com/minio-io/minio/pkg/iodine"
|
||||
@@ -31,6 +33,60 @@ import (
|
||||
|
||||
/// Bucket API operations
|
||||
|
||||
// ListSingle - list delimited path not recursive
|
||||
func (c *s3Client) ListSingle() <-chan client.ItemOnChannel {
|
||||
itemCh := make(chan client.ItemOnChannel)
|
||||
go c.listSingle(itemCh)
|
||||
return itemCh
|
||||
}
|
||||
|
||||
func (c *s3Client) listSingle(itemCh chan client.ItemOnChannel) {
|
||||
defer close(itemCh)
|
||||
var items []*client.Item
|
||||
bucket, objectPrefix := c.url2BucketAndObject()
|
||||
item, err := c.getObjectMetadata(bucket, objectPrefix)
|
||||
switch err {
|
||||
case nil: // List a single object. Exact key
|
||||
itemCh <- client.ItemOnChannel{
|
||||
Item: item,
|
||||
Err: nil,
|
||||
}
|
||||
default:
|
||||
if bucket == "" {
|
||||
items, err = c.listBucketsInternal()
|
||||
if err != nil {
|
||||
itemCh <- client.ItemOnChannel{
|
||||
Item: nil,
|
||||
Err: iodine.New(err, nil),
|
||||
}
|
||||
return
|
||||
}
|
||||
for _, item := range items {
|
||||
itemCh <- client.ItemOnChannel{
|
||||
Item: item,
|
||||
Err: nil,
|
||||
}
|
||||
}
|
||||
return
|
||||
}
|
||||
// List all objects matching the key prefix
|
||||
items, err = c.listObjectsInternal(bucket, "", objectPrefix, "/", globalMaxKeys)
|
||||
if err != nil {
|
||||
itemCh <- client.ItemOnChannel{
|
||||
Item: nil,
|
||||
Err: iodine.New(err, nil),
|
||||
}
|
||||
return
|
||||
}
|
||||
for _, item := range items {
|
||||
itemCh <- client.ItemOnChannel{
|
||||
Item: item,
|
||||
Err: nil,
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// List - list buckets and objects
|
||||
func (c *s3Client) List() <-chan client.ItemOnChannel {
|
||||
itemCh := make(chan client.ItemOnChannel)
|
||||
@@ -177,6 +233,16 @@ func (c *s3Client) listObjectsInternal(bucket, startAt, prefix, delimiter string
|
||||
if err != nil {
|
||||
return nil, iodine.New(err, nil)
|
||||
}
|
||||
for _, prefix := range bres.CommonPrefixes {
|
||||
item := &client.Item{
|
||||
Name: prefix.Prefix,
|
||||
// TODO no way of fixiing this as of now
|
||||
Time: time.Now(),
|
||||
Size: 0,
|
||||
FileType: os.ModeDir,
|
||||
}
|
||||
items = append(items, item)
|
||||
}
|
||||
if !bres.IsTruncated {
|
||||
break
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user