From 972c476e36e2d31354bf13efe148a4dc46dc7655 Mon Sep 17 00:00:00 2001 From: Harshavardhana Date: Thu, 23 Apr 2015 20:02:48 -0700 Subject: [PATCH] Add ListOnChannel() for fs{unix,windows} and cmd-ls --- cmd-ls.go | 19 ++++--------------- cmd_test.go | 27 +++++++++++++++------------ pkg/client/client.go | 20 +++++--------------- pkg/client/fs/fs_unix.go | 30 +++++++++++++++++++++++++----- pkg/client/fs/fs_windows.go | 30 +++++++++++++++++++++++++----- 5 files changed, 74 insertions(+), 52 deletions(-) diff --git a/cmd-ls.go b/cmd-ls.go index bf1bcfa0..c71dbb56 100644 --- a/cmd-ls.go +++ b/cmd-ls.go @@ -59,21 +59,10 @@ func doList(clnt client.Client, targetURL string) (string, error) { } printItem(itemOnChannel.Item.Time, itemOnChannel.Item.Size, itemOnChannel.Item.Name) } - switch iodine.ToError(err).(type) { - case client.APINotImplemented: - items, err := clnt.List() - if err != nil { - err = iodine.New(err, nil) - msg := fmt.Sprintf("mc: listing objects for URL [%s] failed with following reason: [%s]\n", targetURL, iodine.ToError(err)) - return msg, err - } - printItems(items) - default: - if err != nil { - err = iodine.New(err, nil) - msg := fmt.Sprintf("mc: listing objects for URL [%s] failed with following reason: [%s]\n", targetURL, iodine.ToError(err)) - return msg, err - } + if err != nil { + err = iodine.New(err, nil) + msg := fmt.Sprintf("mc: listing objects for URL [%s] failed with following reason: [%s]\n", targetURL, iodine.ToError(err)) + return msg, err } return "", nil } diff --git a/cmd_test.go b/cmd_test.go index f43d02aa..340853b3 100644 --- a/cmd_test.go +++ b/cmd_test.go @@ -292,13 +292,14 @@ func (s *CmdTestSuite) TestLsCmdWithBucket(c *C) { itemCh := make(chan client.ItemOnChannel) go func() { defer close(itemCh) - itemCh <- client.ItemOnChannel{ - Item: nil, - Err: client.APINotImplemented{}, + for _, item := range items { + itemCh <- client.ItemOnChannel{ + Item: item, + Err: nil, + } } }() cl1.On("ListOnChannel").Return(itemCh).Once() - cl1.On("List").Return(items, nil).Once() msg, err := doListCmd(manager, sourceURL, sourceConfig, false) c.Assert(msg, Equals, "") c.Assert(err, IsNil) @@ -335,13 +336,14 @@ func (s *CmdTestSuite) TestLsCmdWithFilePath(c *C) { itemCh := make(chan client.ItemOnChannel) go func() { defer close(itemCh) - itemCh <- client.ItemOnChannel{ - Item: nil, - Err: client.APINotImplemented{}, + for _, item := range items { + itemCh <- client.ItemOnChannel{ + Item: item, + Err: nil, + } } }() cl1.On("ListOnChannel").Return(itemCh).Once() - cl1.On("List").Return(items, nil).Once() msg, err := doListCmd(manager, sourceURL, sourceConfig, false) c.Assert(msg, Equals, "") c.Assert(err, IsNil) @@ -372,13 +374,14 @@ func (s *CmdTestSuite) TestLsCmdListsBuckets(c *C) { itemCh := make(chan client.ItemOnChannel) go func() { defer close(itemCh) - itemCh <- client.ItemOnChannel{ - Item: nil, - Err: client.APINotImplemented{}, + for _, bucket := range buckets { + itemCh <- client.ItemOnChannel{ + Item: bucket, + Err: nil, + } } }() cl1.On("ListOnChannel").Return(itemCh).Once() - cl1.On("List").Return(buckets, nil).Once() msg, err := doListCmd(manager, sourceURL, sourceConfig, false) c.Assert(msg, Equals, "") c.Assert(err, IsNil) diff --git a/pkg/client/client.go b/pkg/client/client.go index 303e157b..9d4b00cc 100644 --- a/pkg/client/client.go +++ b/pkg/client/client.go @@ -18,6 +18,7 @@ package client import ( "io" + "os" "regexp" "time" "unicode/utf8" @@ -75,9 +76,10 @@ type ItemOnChannel struct { // Item - object item list type Item struct { - Name string - Time time.Time - Size int64 + Name string + Time time.Time + Size int64 + FileType os.FileMode } // IsValidBucketName reports whether bucket is a valid bucket name, per Amazon's naming restrictions. @@ -97,18 +99,6 @@ func IsValidBucketName(bucket string) bool { return match } -// BySize implements sort.Interface for []Item based on the Size field. -type BySize []*Item - -// Len - -func (a BySize) Len() int { return len(a) } - -// Swap - -func (a BySize) Swap(i, j int) { a[i], a[j] = a[j], a[i] } - -// Less - -func (a BySize) Less(i, j int) bool { return a[i].Size < a[j].Size } - // IsValidObject - verify object name in accordance with // - http://docs.aws.amazon.com/AmazonS3/latest/dev/UsingMetadata.html func IsValidObject(object string) bool { diff --git a/pkg/client/fs/fs_unix.go b/pkg/client/fs/fs_unix.go index 380a1a1b..d92d4fc8 100644 --- a/pkg/client/fs/fs_unix.go +++ b/pkg/client/fs/fs_unix.go @@ -22,7 +22,6 @@ import ( "io" "os" "path/filepath" - "sort" "strings" "io/ioutil" @@ -118,9 +117,31 @@ func (f *fsClient) ListOnChannel() <-chan client.ItemOnChannel { func (f *fsClient) listInGoroutine(itemCh chan client.ItemOnChannel) { defer close(itemCh) - itemCh <- client.ItemOnChannel{ - Item: nil, - Err: iodine.New(client.APINotImplemented{API: "ListOnChannel"}, nil), + visitFS := func(fp string, fi os.FileInfo, err error) error { + if err != nil { + if os.IsPermission(err) { // skip inaccessible files + return nil + } + return err // fatal + } + item := &client.Item{ + Name: fp, + Time: fi.ModTime(), + Size: fi.Size(), + FileType: fi.Mode(), + } + itemCh <- client.ItemOnChannel{ + Item: item, + Err: nil, + } + return nil + } + err := filepath.Walk(f.path, visitFS) + if err != nil { + itemCh <- client.ItemOnChannel{ + Item: nil, + Err: iodine.New(err, nil), + } } } @@ -146,7 +167,6 @@ func (f *fsClient) List() (items []*client.Item, err error) { if err != nil { return nil, iodine.New(err, nil) } - sort.Sort(client.BySize(items)) return items, nil } diff --git a/pkg/client/fs/fs_windows.go b/pkg/client/fs/fs_windows.go index d17ad574..07aaaa01 100644 --- a/pkg/client/fs/fs_windows.go +++ b/pkg/client/fs/fs_windows.go @@ -19,7 +19,6 @@ package fs import ( "io" "os" - "sort" "strings" "io/ioutil" @@ -133,9 +132,31 @@ func (f *fsClient) ListOnChannel() <-chan client.ItemOnChannel { func (f *fsClient) listInGoroutine(itemCh chan client.ItemOnChannel) { defer close(itemCh) - itemCh <- client.ItemOnChannel{ - Item: nil, - Err: iodine.New(client.APINotImplemented{API: "ListOnChannel"}, nil), + visitFS := func(fp string, fi os.FileInfo, err error) error { + if err != nil { + if os.IsPermission(err) { // skip inaccessible files + return nil + } + return err // fatal + } + item := &client.Item{ + Name: fp, + Time: fi.ModTime(), + Size: fi.Size(), + FileType: fi.Mode(), + } + itemCh <- client.ItemOnChannel{ + Item: item, + Err: nil, + } + return nil + } + err := filepath.Walk(f.path, visitFS) + if err != nil { + itemCh <- client.ItemOnChannel{ + Item: nil, + Err: iodine.New(err, nil), + } } } @@ -166,7 +187,6 @@ func (f *fsClient) List() (items []*client.Item, err error) { if err != nil { return nil, iodine.New(err, nil) } - sort.Sort(client.BySize(items)) return items, nil } }