From 1c2f292f779e3991abae92b4cc416773b3340d1d Mon Sep 17 00:00:00 2001 From: Harshavardhana Date: Tue, 17 Nov 2015 12:55:57 -0800 Subject: [PATCH] ls: listing fixes for all the bugs we found - fixes #1356 - fixes #1355 - fixes #1358 --- cp-url.go | 6 +- diff-main.go | 8 +- difference.go | 14 +-- ls.go | 23 ++-- mirror-url.go | 10 +- pkg/client/client.go | 9 +- pkg/client/fs/fs.go | 234 +++++++++++++++------------------------ pkg/client/fs/fs_test.go | 24 ++-- pkg/client/s3/s3.go | 138 +++++++++-------------- pkg/client/s3/s3_test.go | 6 +- rm-main.go | 35 ++++-- share-download-main.go | 14 ++- url.go | 2 +- 13 files changed, 223 insertions(+), 300 deletions(-) diff --git a/cp-url.go b/cp-url.go index f3610e0d..df5d12e5 100644 --- a/cp-url.go +++ b/cp-url.go @@ -281,21 +281,21 @@ func prepareCopyURLsTypeC(sourceURL, targetURL string) <-chan copyURLs { continue } - if !sourceContent.Content.Type.IsRegular() { + if !sourceContent.Type.IsRegular() { // Source is not a regular file. Skip it for copy. continue } // All OK.. We can proceed. Type B: source is a file, target is a folder and exists. srcURL := sourceClient.GetURL() - newSourceURL := sourceContent.Content.URL + newSourceURL := sourceContent.URL pathSeparatorIndex := strings.LastIndex(srcURL.Path, string(srcURL.Separator)) newSourceSuffix := newSourceURL.Path if pathSeparatorIndex > 1 { newSourceSuffix = strings.TrimPrefix(newSourceURL.Path, srcURL.Path[:pathSeparatorIndex]) } newTargetURL := urlJoinPath(targetURL, newSourceSuffix) - copyURLsCh <- prepareCopyURLsTypeA(sourceContent.Content.URL.String(), newTargetURL) + copyURLsCh <- prepareCopyURLsTypeA(sourceContent.URL.String(), newTargetURL) } }(sourceURL, targetURL, copyURLsCh) return copyURLsCh diff --git a/diff-main.go b/diff-main.go index 93a33d3b..72138ce1 100644 --- a/diff-main.go +++ b/diff-main.go @@ -134,11 +134,11 @@ func doDiffMain(firstURL, secondURL string) <-chan diffMessage { ch <- diffMessage{Error: sourceContent.Err.Trace()} continue } - if sourceContent.Content.Type.IsDir() { + if sourceContent.Type.IsDir() { continue } - suffix := strings.TrimPrefix(sourceContent.Content.URL.String(), firstURL) - differ, err := difference(suffix, sourceContent.Content.Type, sourceContent.Content.Size) + suffix := strings.TrimPrefix(sourceContent.URL.String(), firstURL) + differ, err := difference(suffix, sourceContent.Type, sourceContent.Size) if err != nil { ch <- diffMessage{Error: err.Trace()} continue @@ -147,7 +147,7 @@ func doDiffMain(firstURL, secondURL string) <-chan diffMessage { continue } ch <- diffMessage{ - FirstURL: sourceContent.Content.URL.String(), + FirstURL: sourceContent.URL.String(), SecondURL: urlJoinPath(secondURL, suffix), Diff: differ, } diff --git a/difference.go b/difference.go index 9cfeef6e..8d9447b1 100644 --- a/difference.go +++ b/difference.go @@ -49,7 +49,7 @@ func objectDifferenceFactory(targetURL string) (objectDifference, *probe.Error) current := targetURL reachedEOF := false ok := false - var content client.ContentOnChannel + var content *client.Content difference := func(suffix string, srcType os.FileMode, srcSize int64) (string, *probe.Error) { if reachedEOF { @@ -62,8 +62,8 @@ func objectDifferenceFactory(targetURL string) (objectDifference, *probe.Error) return differOnlyFirst, nil // not available in the target } if expected == current { - tgtType := content.Content.Type - tgtSize := content.Content.Size + tgtType := content.Type + tgtSize := content.Size if srcType.IsRegular() && !tgtType.IsRegular() { // Type differes. Source is never a directory return differType, nil @@ -75,14 +75,14 @@ func objectDifferenceFactory(targetURL string) (objectDifference, *probe.Error) return differNone, nil // available in the target } content, ok = <-ch - if content.Err != nil { - return "", content.Err.Trace() - } if !ok { reachedEOF = true return differOnlyFirst, nil } - current = content.Content.URL.String() + if content.Err != nil { + return "", content.Err.Trace() + } + current = content.URL.String() } } return difference, nil diff --git a/ls.go b/ls.go index 7d6fbd59..f1679088 100644 --- a/ls.go +++ b/ls.go @@ -132,35 +132,32 @@ func doList(clnt client.Client, isRecursive, isIncomplete bool) *probe.Error { if err != nil { return err.Trace(clnt.GetURL().String()) } - for contentCh := range clnt.List(isRecursive, isIncomplete) { - if contentCh.Err != nil { - switch contentCh.Err.ToGoError().(type) { + for content := range clnt.List(isRecursive, isIncomplete) { + if content.Err != nil { + switch content.Err.ToGoError().(type) { // handle this specifically for filesystem related errors. case client.BrokenSymlink: - errorIf(contentCh.Err.Trace(), "Unable to list broken link.") + errorIf(content.Err.Trace(), "Unable to list broken link.") continue case client.TooManyLevelsSymlink: - errorIf(contentCh.Err.Trace(), "Unable to list too many levels link.") + errorIf(content.Err.Trace(), "Unable to list too many levels link.") continue case client.PathNotFound: - errorIf(contentCh.Err.Trace(), "Unable to list folder.") + errorIf(content.Err.Trace(), "Unable to list folder.") continue case client.PathInsufficientPermission: - errorIf(contentCh.Err.Trace(), "Unable to list folder.") + errorIf(content.Err.Trace(), "Unable to list folder.") continue } - err = contentCh.Err.Trace() - break + errorIf(content.Err.Trace(), "Unable to list folder.") + continue } // trim incoming content based on if its recursive or not. - trimmedContent := trimContent(parentContentDir, contentCh.Content) + trimmedContent := trimContent(parentContentDir, content) // parse trimmed content into printable form. parsedContent := parseContent(trimmedContent) // print colorized or jsonized content info. printMsg(parsedContent) } - if err != nil { - return err.Trace() - } return nil } diff --git a/mirror-url.go b/mirror-url.go index 2c9f7d66..30f4c0ab 100644 --- a/mirror-url.go +++ b/mirror-url.go @@ -131,13 +131,13 @@ func deltaSourceTargets(sourceURL string, targetURLs []string, mirrorURLsCh chan mirrorURLsCh <- mirrorURLs{Error: sourceContent.Err.Trace()} continue } - if sourceContent.Content.Type.IsDir() { + if sourceContent.Type.IsDir() { continue } - suffix := strings.TrimPrefix(sourceContent.Content.URL.String(), sourceURL) + suffix := strings.TrimPrefix(sourceContent.URL.String(), sourceURL) targetContents := []*client.Content{} for i, difference := range objectDifferenceArray { - differ, err := difference(suffix, sourceContent.Content.Type, sourceContent.Content.Size) + differ, err := difference(suffix, sourceContent.Type, sourceContent.Size) if err != nil { mirrorURLsCh <- mirrorURLs{Error: err.Trace()} continue @@ -152,7 +152,7 @@ func deltaSourceTargets(sourceURL string, targetURLs []string, mirrorURLsCh chan } if differ == differSize && !mirrorIsForce { // size differs and force not set - mirrorURLsCh <- mirrorURLs{Error: errOverWriteNotAllowed(sourceContent.Content.URL.String())} + mirrorURLsCh <- mirrorURLs{Error: errOverWriteNotAllowed(sourceContent.URL.String())} continue } // either available only in source or size differs and force is set @@ -162,7 +162,7 @@ func deltaSourceTargets(sourceURL string, targetURLs []string, mirrorURLsCh chan } if len(targetContents) > 0 { mirrorURLsCh <- mirrorURLs{ - SourceContent: sourceContent.Content, + SourceContent: sourceContent, TargetContents: targetContents, } } diff --git a/pkg/client/client.go b/pkg/client/client.go index 421efd90..187effa4 100644 --- a/pkg/client/client.go +++ b/pkg/client/client.go @@ -28,7 +28,7 @@ import ( type Client interface { // Common operations Stat() (content *Content, err *probe.Error) - List(recursive, incomplete bool) <-chan ContentOnChannel + List(recursive, incomplete bool) <-chan *Content // Bucket operations MakeBucket() *probe.Error @@ -50,18 +50,13 @@ type Client interface { GetURL() URL } -// ContentOnChannel - List contents on channel -type ContentOnChannel struct { - Content *Content - Err *probe.Error -} - // Content container for content metadata type Content struct { URL URL Time time.Time Size int64 Type os.FileMode + Err *probe.Error } // Config - see http://docs.amazonwebservices.com/AmazonS3/latest/dev/index.html?RESTAuthentication.html diff --git a/pkg/client/fs/fs.go b/pkg/client/fs/fs.go index 7d20fb34..a263eff4 100644 --- a/pkg/client/fs/fs.go +++ b/pkg/client/fs/fs.go @@ -219,8 +219,8 @@ func (f *fsClient) Remove(incomplete bool) *probe.Error { } // List - list files and folders -func (f *fsClient) List(recursive, incomplete bool) <-chan client.ContentOnChannel { - contentCh := make(chan client.ContentOnChannel) +func (f *fsClient) List(recursive, incomplete bool) <-chan *client.Content { + contentCh := make(chan *client.Content) if incomplete { go func() { defer close(contentCh) @@ -242,7 +242,7 @@ func (b byName) Len() int { return len(b) } func (b byName) Swap(i, j int) { b[i], b[j] = b[j], b[i] } func (b byName) Less(i, j int) bool { return b[i].Name() < b[j].Name() } -func (f *fsClient) listInRoutine(contentCh chan client.ContentOnChannel) { +func (f *fsClient) listInRoutine(contentCh chan *client.Content) { defer close(contentCh) pathURL := *f.PathURL @@ -253,17 +253,12 @@ func (f *fsClient) listInRoutine(contentCh chan client.ContentOnChannel) { if _, ok := err.ToGoError().(client.PathNotFound); ok { dir, err := os.Open(filepath.Dir(fpath)) if err != nil { - contentCh <- client.ContentOnChannel{ - Content: nil, - Err: probe.NewError(err), - } + contentCh <- &client.Content{Err: probe.NewError(err)} + return } files, err := dir.Readdir(-1) if err != nil { - contentCh <- client.ContentOnChannel{ - Content: nil, - Err: probe.NewError(err), - } + contentCh <- &client.Content{Err: probe.NewError(err)} return } // NOTE: This will be slow for large directories. @@ -273,57 +268,44 @@ func (f *fsClient) listInRoutine(contentCh chan client.ContentOnChannel) { if fi.Mode()&os.ModeSymlink == os.ModeSymlink { st, err := os.Stat(file) if err != nil { - contentCh <- client.ContentOnChannel{ - Content: nil, - Err: probe.NewError(err), - } + contentCh <- &client.Content{Err: probe.NewError(err)} } if strings.HasPrefix(file, fpath) { - contentCh <- client.ContentOnChannel{ - Content: &client.Content{ - URL: *client.NewURL(file), - Time: st.ModTime(), - Size: st.Size(), - Type: st.Mode(), - }, - Err: nil, + contentCh <- &client.Content{ + URL: *client.NewURL(file), + Time: st.ModTime(), + Size: st.Size(), + Type: st.Mode(), + Err: nil, } continue } } if strings.HasPrefix(file, fpath) { - contentCh <- client.ContentOnChannel{ - Content: &client.Content{ - URL: *client.NewURL(file), - Time: fi.ModTime(), - Size: fi.Size(), - Type: fi.Mode(), - }, - Err: nil, + contentCh <- &client.Content{ + URL: *client.NewURL(file), + Time: fi.ModTime(), + Size: fi.Size(), + Type: fi.Mode(), + Err: nil, } } } return } // if client.PathNotFound fails we return genuine error back to the caller. - contentCh <- client.ContentOnChannel{ - Content: nil, - Err: err.Trace(fpath), - } + contentCh <- &client.Content{Err: err.Trace(fpath)} return } // if the directory doesn't end with a separator, do not traverse it. if !strings.HasSuffix(fpath, string(pathURL.Separator)) && fst.Mode().IsDir() && fpath != "." { - content := &client.Content{ + contentCh <- &client.Content{ URL: pathURL, Time: fst.ModTime(), Size: fst.Size(), Type: fst.Mode(), - } - contentCh <- client.ContentOnChannel{ - Content: content, - Err: nil, + Err: nil, } return } @@ -337,20 +319,14 @@ func (f *fsClient) listInRoutine(contentCh chan client.ContentOnChannel) { // quantities of files dir, err := os.Open(fpath) if err != nil { - contentCh <- client.ContentOnChannel{ - Content: nil, - Err: probe.NewError(err), - } + contentCh <- &client.Content{Err: probe.NewError(err)} return } defer dir.Close() files, err := dir.Readdir(-1) if err != nil { - contentCh <- client.ContentOnChannel{ - Content: nil, - Err: probe.NewError(err), - } + contentCh <- &client.Content{Err: probe.NewError(err)} return } // NOTE: This will be slow for large directories. @@ -369,49 +345,40 @@ func (f *fsClient) listInRoutine(contentCh chan client.ContentOnChannel) { lfi, lerr := os.Lstat(newPath) if lerr != nil { if os.IsPermission(lerr) { - contentCh <- client.ContentOnChannel{ - Content: nil, - Err: probe.NewError(client.PathInsufficientPermission{Path: newPath}), + contentCh <- &client.Content{ + Err: probe.NewError(client.PathInsufficientPermission{Path: newPath}), } continue } - contentCh <- client.ContentOnChannel{ - Content: nil, - Err: probe.NewError(lerr), + contentCh <- &client.Content{ + Err: probe.NewError(lerr), } continue } pathURL := *f.PathURL pathURL.Path = filepath.Join(pathURL.Path, lfi.Name()) - contentCh <- client.ContentOnChannel{ - Content: &client.Content{ - URL: pathURL, - Time: lfi.ModTime(), - Size: lfi.Size(), - Type: lfi.Mode(), - }, - Err: probe.NewError(client.PathInsufficientPermission{Path: pathURL.Path}), + contentCh <- &client.Content{ + URL: pathURL, + Time: lfi.ModTime(), + Size: lfi.Size(), + Type: lfi.Mode(), + Err: probe.NewError(client.PathInsufficientPermission{Path: pathURL.Path}), } continue } else { - contentCh <- client.ContentOnChannel{ - Content: nil, - Err: probe.NewError(err), - } + contentCh <- &client.Content{Err: probe.NewError(err)} continue } } if os.IsNotExist(err) { - contentCh <- client.ContentOnChannel{ - Content: nil, - Err: probe.NewError(client.BrokenSymlink{Path: file.Name()}), + contentCh <- &client.Content{ + Err: probe.NewError(client.BrokenSymlink{Path: file.Name()}), } continue } if err != nil { - contentCh <- client.ContentOnChannel{ - Content: nil, - Err: probe.NewError(err), + contentCh <- &client.Content{ + Err: probe.NewError(err), } continue } @@ -419,33 +386,27 @@ func (f *fsClient) listInRoutine(contentCh chan client.ContentOnChannel) { if fi.Mode().IsRegular() || fi.Mode().IsDir() { pathURL := *f.PathURL pathURL.Path = filepath.Join(pathURL.Path, fi.Name()) - content := &client.Content{ + contentCh <- &client.Content{ URL: pathURL, Time: fi.ModTime(), Size: fi.Size(), Type: fi.Mode(), - } - contentCh <- client.ContentOnChannel{ - Content: content, - Err: nil, + Err: nil, } } } default: - content := &client.Content{ + contentCh <- &client.Content{ URL: pathURL, Time: fst.ModTime(), Size: fst.Size(), Type: fst.Mode(), - } - contentCh <- client.ContentOnChannel{ - Content: content, - Err: nil, + Err: nil, } } } -func (f *fsClient) listRecursiveInRoutine(contentCh chan client.ContentOnChannel) { +func (f *fsClient) listRecursiveInRoutine(contentCh chan *client.Content) { defer close(contentCh) var dirName string var filePrefix string @@ -464,26 +425,30 @@ func (f *fsClient) listRecursiveInRoutine(contentCh chan client.ContentOnChannel // we should not skip file or directory during two situations: (ex. mc ls /usr/bi...) // 1. when fp is /usr and prefix is /usr/bi // 2. when fp is /usr/bin/subdir and prefix is /usr/bi - if !strings.HasPrefix(fp, filePrefix) && - !strings.HasPrefix(filePrefix, fp) { - if err == nil { - if fi.IsDir() { - return ErrSkipDir + // 3. Do not check filePrefix if its '.' + if filePrefix != "." { + if !strings.HasPrefix(fp, filePrefix) && + !strings.HasPrefix(filePrefix, fp) { + if err == nil { + if fi.IsDir() { + return ErrSkipDir + } + return nil + } + } + // Skip when fp is /usr and prefix is /usr/bi + // Do not check filePrefix if its '.' + if filePrefix != "." { + if !strings.HasPrefix(fp, filePrefix) { + return nil } - return nil } - } - - // Skip when fp is /usr and prefix is /usr/bi - if !strings.HasPrefix(fp, filePrefix) { - return nil } if err != nil { if strings.Contains(err.Error(), "operation not permitted") { - contentCh <- client.ContentOnChannel{ - Content: nil, - Err: probe.NewError(err), + contentCh <- &client.Content{ + Err: probe.NewError(err), } return nil } @@ -495,27 +460,23 @@ func (f *fsClient) listRecursiveInRoutine(contentCh chan client.ContentOnChannel // - which cannot be accessed with regular operations lfi, lerr := os.Lstat(fp) if lerr != nil { - contentCh <- client.ContentOnChannel{ - Content: nil, - Err: probe.NewError(lerr), + contentCh <- &client.Content{ + Err: probe.NewError(lerr), } return nil } pathURL := *f.PathURL pathURL.Path = filepath.Join(pathURL.Path, dirName) - contentCh <- client.ContentOnChannel{ - Content: &client.Content{ - URL: pathURL, - Time: lfi.ModTime(), - Size: lfi.Size(), - Type: lfi.Mode(), - }, - Err: probe.NewError(err), + contentCh <- &client.Content{ + URL: pathURL, + Time: lfi.ModTime(), + Size: lfi.Size(), + Type: lfi.Mode(), + Err: probe.NewError(err), } } else { - contentCh <- client.ContentOnChannel{ - Content: nil, - Err: probe.NewError(client.PathInsufficientPermission{Path: fp}), + contentCh <- &client.Content{ + Err: probe.NewError(client.PathInsufficientPermission{Path: fp}), } } return nil @@ -534,48 +495,41 @@ func (f *fsClient) listRecursiveInRoutine(contentCh chan client.ContentOnChannel lfi, lerr := os.Lstat(fp) if lerr != nil { if os.IsPermission(lerr) { - contentCh <- client.ContentOnChannel{ - Content: nil, - Err: probe.NewError(client.PathInsufficientPermission{Path: fp}), + contentCh <- &client.Content{ + Err: probe.NewError(client.PathInsufficientPermission{Path: fp}), } return nil } - contentCh <- client.ContentOnChannel{ - Content: nil, - Err: probe.NewError(lerr), + contentCh <- &client.Content{ + Err: probe.NewError(lerr), } return nil } pathURL := *f.PathURL pathURL.Path = filepath.Join(pathURL.Path, dirName) - contentCh <- client.ContentOnChannel{ - Content: &client.Content{ - URL: pathURL, - Time: lfi.ModTime(), - Size: lfi.Size(), - Type: lfi.Mode(), - }, - Err: probe.NewError(err), + contentCh <- &client.Content{ + URL: pathURL, + Time: lfi.ModTime(), + Size: lfi.Size(), + Type: lfi.Mode(), + Err: probe.NewError(err), } return nil } - contentCh <- client.ContentOnChannel{ - Content: nil, - Err: probe.NewError(err), + contentCh <- &client.Content{ + Err: probe.NewError(err), } return nil } if os.IsNotExist(err) { // ignore broken symlinks - contentCh <- client.ContentOnChannel{ - Content: nil, - Err: probe.NewError(client.BrokenSymlink{Path: fp}), + contentCh <- &client.Content{ + Err: probe.NewError(client.BrokenSymlink{Path: fp}), } return nil } if strings.Contains(err.Error(), "too many levels of symbolic links") { - contentCh <- client.ContentOnChannel{ - Content: nil, - Err: probe.NewError(client.TooManyLevelsSymlink{Path: fp}), + contentCh <- &client.Content{ + Err: probe.NewError(client.TooManyLevelsSymlink{Path: fp}), } return nil } @@ -583,15 +537,12 @@ func (f *fsClient) listRecursiveInRoutine(contentCh chan client.ContentOnChannel } } if fi.Mode().IsRegular() || fi.Mode().IsDir() { - content := &client.Content{ + contentCh <- &client.Content{ URL: *client.NewURL(fp), Time: fi.ModTime(), Size: fi.Size(), Type: fi.Mode(), - } - contentCh <- client.ContentOnChannel{ - Content: content, - Err: nil, + Err: nil, } } return nil @@ -614,9 +565,8 @@ func (f *fsClient) listRecursiveInRoutine(contentCh chan client.ContentOnChannel } err := Walk(dirName, visitFS) if err != nil { - contentCh <- client.ContentOnChannel{ - Content: nil, - Err: probe.NewError(err), + contentCh <- &client.Content{ + Err: probe.NewError(err), } } } diff --git a/pkg/client/fs/fs_test.go b/pkg/client/fs/fs_test.go index 1f9ac3bb..b89ba5a1 100644 --- a/pkg/client/fs/fs_test.go +++ b/pkg/client/fs/fs_test.go @@ -61,12 +61,12 @@ func (s *MySuite) TestList(c *C) { c.Assert(err, IsNil) var contents []*client.Content - for contentCh := range fsc.List(false, false) { - if contentCh.Err != nil { - err = contentCh.Err + for content := range fsc.List(false, false) { + if content.Err != nil { + err = content.Err break } - contents = append(contents, contentCh.Content) + contents = append(contents, content) } c.Assert(err, IsNil) c.Assert(len(contents), Equals, 1) @@ -83,12 +83,12 @@ func (s *MySuite) TestList(c *C) { c.Assert(err, IsNil) contents = nil - for contentCh := range fsc.List(false, false) { - if contentCh.Err != nil { - err = contentCh.Err + for content := range fsc.List(false, false) { + if content.Err != nil { + err = content.Err break } - contents = append(contents, contentCh.Content) + contents = append(contents, content) } c.Assert(err, IsNil) c.Assert(len(contents), Equals, 1) @@ -98,12 +98,12 @@ func (s *MySuite) TestList(c *C) { c.Assert(err, IsNil) contents = nil - for contentCh := range fsc.List(true, false) { - if contentCh.Err != nil { - err = contentCh.Err + for content := range fsc.List(true, false) { + if content.Err != nil { + err = content.Err break } - contents = append(contents, contentCh.Content) + contents = append(contents, content) } c.Assert(err, IsNil) diff --git a/pkg/client/s3/s3.go b/pkg/client/s3/s3.go index 9db55132..5ee53fdf 100644 --- a/pkg/client/s3/s3.go +++ b/pkg/client/s3/s3.go @@ -312,11 +312,11 @@ func (c *s3Client) url2BucketAndObject() (bucketName, objectName string) { /// Bucket API operations // List - list at delimited path, if not recursive -func (c *s3Client) List(recursive, incomplete bool) <-chan client.ContentOnChannel { +func (c *s3Client) List(recursive, incomplete bool) <-chan *client.Content { c.mu.Lock() defer c.mu.Unlock() - contentCh := make(chan client.ContentOnChannel) + contentCh := make(chan *client.Content) if incomplete { if recursive { go c.listIncompleteRecursiveInRoutine(contentCh) @@ -333,7 +333,7 @@ func (c *s3Client) List(recursive, incomplete bool) <-chan client.ContentOnChann return contentCh } -func (c *s3Client) listIncompleteInRoutine(contentCh chan client.ContentOnChannel) { +func (c *s3Client) listIncompleteInRoutine(contentCh chan *client.Content) { defer close(contentCh) // get bucket and object from URL b, o := c.url2BucketAndObject() @@ -341,17 +341,15 @@ func (c *s3Client) listIncompleteInRoutine(contentCh chan client.ContentOnChanne case b == "" && o == "": for bucket := range c.api.ListBuckets() { if bucket.Err != nil { - contentCh <- client.ContentOnChannel{ - Content: nil, - Err: probe.NewError(bucket.Err), + contentCh <- &client.Content{ + Err: probe.NewError(bucket.Err), } return } for object := range c.api.ListIncompleteUploads(bucket.Stat.Name, o, false) { if object.Err != nil { - contentCh <- client.ContentOnChannel{ - Content: nil, - Err: probe.NewError(object.Err), + contentCh <- &client.Content{ + Err: probe.NewError(object.Err), } return } @@ -374,18 +372,14 @@ func (c *s3Client) listIncompleteInRoutine(contentCh chan client.ContentOnChanne content.Time = object.Stat.Initiated content.Type = os.ModeTemporary } - contentCh <- client.ContentOnChannel{ - Content: content, - Err: nil, - } + contentCh <- content } } default: for object := range c.api.ListIncompleteUploads(b, o, false) { if object.Err != nil { - contentCh <- client.ContentOnChannel{ - Content: nil, - Err: probe.NewError(object.Err), + contentCh <- &client.Content{ + Err: probe.NewError(object.Err), } return } @@ -408,15 +402,12 @@ func (c *s3Client) listIncompleteInRoutine(contentCh chan client.ContentOnChanne content.Time = object.Stat.Initiated content.Type = os.ModeTemporary } - contentCh <- client.ContentOnChannel{ - Content: content, - Err: nil, - } + contentCh <- content } } } -func (c *s3Client) listIncompleteRecursiveInRoutine(contentCh chan client.ContentOnChannel) { +func (c *s3Client) listIncompleteRecursiveInRoutine(contentCh chan *client.Content) { defer close(contentCh) // get bucket and object from URL b, o := c.url2BucketAndObject() @@ -424,17 +415,15 @@ func (c *s3Client) listIncompleteRecursiveInRoutine(contentCh chan client.Conten case b == "" && o == "": for bucket := range c.api.ListBuckets() { if bucket.Err != nil { - contentCh <- client.ContentOnChannel{ - Content: nil, - Err: probe.NewError(bucket.Err), + contentCh <- &client.Content{ + Err: probe.NewError(bucket.Err), } return } for object := range c.api.ListIncompleteUploads(bucket.Stat.Name, o, true) { if object.Err != nil { - contentCh <- client.ContentOnChannel{ - Content: nil, - Err: probe.NewError(object.Err), + contentCh <- &client.Content{ + Err: probe.NewError(object.Err), } return } @@ -445,18 +434,14 @@ func (c *s3Client) listIncompleteRecursiveInRoutine(contentCh chan client.Conten content.Size = object.Stat.Size content.Time = object.Stat.Initiated content.Type = os.ModeTemporary - contentCh <- client.ContentOnChannel{ - Content: content, - Err: nil, - } + contentCh <- content } } default: for object := range c.api.ListIncompleteUploads(b, o, true) { if object.Err != nil { - contentCh <- client.ContentOnChannel{ - Content: nil, - Err: probe.NewError(object.Err), + contentCh <- &client.Content{ + Err: probe.NewError(object.Err), } return } @@ -471,15 +456,12 @@ func (c *s3Client) listIncompleteRecursiveInRoutine(contentCh chan client.Conten content.Size = object.Stat.Size content.Time = object.Stat.Initiated content.Type = os.ModeTemporary - contentCh <- client.ContentOnChannel{ - Content: content, - Err: nil, - } + contentCh <- content } } } -func (c *s3Client) listInRoutine(contentCh chan client.ContentOnChannel) { +func (c *s3Client) listInRoutine(contentCh chan *client.Content) { defer close(contentCh) // get bucket and object from URL b, o := c.url2BucketAndObject() @@ -487,9 +469,8 @@ func (c *s3Client) listInRoutine(contentCh chan client.ContentOnChannel) { case b == "" && o == "": for bucket := range c.api.ListBuckets() { if bucket.Err != nil { - contentCh <- client.ContentOnChannel{ - Content: nil, - Err: probe.NewError(bucket.Err), + contentCh <- &client.Content{ + Err: probe.NewError(bucket.Err), } return } @@ -500,26 +481,19 @@ func (c *s3Client) listInRoutine(contentCh chan client.ContentOnChannel) { content.Size = 0 content.Time = bucket.Stat.CreationDate content.Type = os.ModeDir - contentCh <- client.ContentOnChannel{ - Content: content, - Err: nil, - } + contentCh <- content } case b != "" && !strings.HasSuffix(c.hostURL.Path, string(c.hostURL.Separator)) && o == "": err := c.api.BucketExists(b) if err != nil { - contentCh <- client.ContentOnChannel{ - Content: nil, - Err: probe.NewError(err), + contentCh <- &client.Content{ + Err: probe.NewError(err), } } content := new(client.Content) content.URL = *c.hostURL content.Type = os.ModeDir - contentCh <- client.ContentOnChannel{ - Content: content, - Err: nil, - } + contentCh <- content default: metadata, err := c.api.StatObject(b, o) switch err.(type) { @@ -529,16 +503,12 @@ func (c *s3Client) listInRoutine(contentCh chan client.ContentOnChannel) { content.Time = metadata.LastModified content.Size = metadata.Size content.Type = os.FileMode(0664) - contentCh <- client.ContentOnChannel{ - Content: content, - Err: nil, - } + contentCh <- content default: for object := range c.api.ListObjects(b, o, false) { if object.Err != nil { - contentCh <- client.ContentOnChannel{ - Content: nil, - Err: probe.NewError(object.Err), + contentCh <- &client.Content{ + Err: probe.NewError(object.Err), } return } @@ -561,16 +531,13 @@ func (c *s3Client) listInRoutine(contentCh chan client.ContentOnChannel) { content.Time = object.Stat.LastModified content.Type = os.FileMode(0664) } - contentCh <- client.ContentOnChannel{ - Content: content, - Err: nil, - } + contentCh <- content } } } } -func (c *s3Client) listRecursiveInRoutine(contentCh chan client.ContentOnChannel) { +func (c *s3Client) listRecursiveInRoutine(contentCh chan *client.Content) { defer close(contentCh) // get bucket and object from URL b, o := c.url2BucketAndObject() @@ -578,41 +545,41 @@ func (c *s3Client) listRecursiveInRoutine(contentCh chan client.ContentOnChannel case b == "" && o == "": for bucket := range c.api.ListBuckets() { if bucket.Err != nil { - contentCh <- client.ContentOnChannel{ - Content: nil, - Err: probe.NewError(bucket.Err), + contentCh <- &client.Content{ + Err: probe.NewError(bucket.Err), } return } + bucketURL := *c.hostURL + bucketURL.Path = filepath.Join(bucketURL.Path, bucket.Stat.Name) + contentCh <- &client.Content{ + URL: bucketURL, + Type: os.ModeDir, + } for object := range c.api.ListObjects(bucket.Stat.Name, o, true) { if object.Err != nil { - contentCh <- client.ContentOnChannel{ - Content: nil, - Err: probe.NewError(object.Err), + contentCh <- &client.Content{ + Err: probe.NewError(object.Err), } - return + continue } content := new(client.Content) - url := *c.hostURL - url.Path = filepath.Join(url.Path, bucket.Stat.Name, object.Stat.Key) - content.URL = url + objectURL := *c.hostURL + objectURL.Path = filepath.Join(objectURL.Path, bucket.Stat.Name, object.Stat.Key) + content.URL = objectURL content.Size = object.Stat.Size content.Time = object.Stat.LastModified content.Type = os.FileMode(0664) - contentCh <- client.ContentOnChannel{ - Content: content, - Err: nil, - } + contentCh <- content } } default: for object := range c.api.ListObjects(b, o, true) { if object.Err != nil { - contentCh <- client.ContentOnChannel{ - Content: nil, - Err: probe.NewError(object.Err), + contentCh <- &client.Content{ + Err: probe.NewError(object.Err), } - return + continue } content := new(client.Content) url := *c.hostURL @@ -626,10 +593,7 @@ func (c *s3Client) listRecursiveInRoutine(contentCh chan client.ContentOnChannel content.Size = object.Stat.Size content.Time = object.Stat.LastModified content.Type = os.FileMode(0664) - contentCh <- client.ContentOnChannel{ - Content: content, - Err: nil, - } + contentCh <- content } } } diff --git a/pkg/client/s3/s3_test.go b/pkg/client/s3/s3_test.go index ce6c4354..97d9a6ab 100644 --- a/pkg/client/s3/s3_test.go +++ b/pkg/client/s3/s3_test.go @@ -149,7 +149,7 @@ func (s *MySuite) TestBucketOperations(c *C) { for content := range s3c.List(false, false) { c.Assert(content.Err, IsNil) - c.Assert(content.Content.Type.IsDir(), Equals, true) + c.Assert(content.Type.IsDir(), Equals, true) } conf.HostURL = server.URL + "/bucket" @@ -158,7 +158,7 @@ func (s *MySuite) TestBucketOperations(c *C) { for content := range s3c.List(false, false) { c.Assert(content.Err, IsNil) - c.Assert(content.Content.Type.IsDir(), Equals, true) + c.Assert(content.Type.IsDir(), Equals, true) } conf.HostURL = server.URL + "/bucket/" @@ -167,7 +167,7 @@ func (s *MySuite) TestBucketOperations(c *C) { for content := range s3c.List(false, false) { c.Assert(content.Err, IsNil) - c.Assert(content.Content.Type.IsRegular(), Equals, true) + c.Assert(content.Type.IsRegular(), Equals, true) } } diff --git a/rm-main.go b/rm-main.go index d80e984d..002d1a8b 100644 --- a/rm-main.go +++ b/rm-main.go @@ -135,18 +135,16 @@ func checkRmSyntax(ctx *cli.Context) { } // Remove a single object. -func rm(url string, isIncomplete bool) { +func rm(url string, isIncomplete bool) *probe.Error { clnt, err := url2Client(url) if err != nil { - errorIf(err.Trace(url), "Invalid URL ‘"+url+"’.") - return + return err.Trace(url) } if err = clnt.Remove(isIncomplete); err != nil { - fatalIf(err.Trace(url), "Unable to remove ‘"+url+"’.") + return err.Trace(url) } - - printMsg(rmMessage{url}) + return nil } // Remove all objects recursively. @@ -164,9 +162,9 @@ func rmAll(url string, isIncomplete bool) { return // End of journey. } - if entry.Content.Type.IsDir() { + if entry.Type.IsDir() { // Add separator at the end to remove all its contents. - url := entry.Content.URL.String() + url := entry.URL.String() u := client.NewURL(url) url = url + string(u.Separator) @@ -174,7 +172,12 @@ func rmAll(url string, isIncomplete bool) { rmAll(url, isIncomplete) } // Regular type. - rm(entry.Content.URL.String(), isIncomplete) + err := rm(entry.URL.String(), isIncomplete) + if err != nil { + errorIf(err.Trace(entry.URL.String()), "Unable to remove ‘"+entry.URL.String()+"’.") + continue + } + printMsg(rmMessage{entry.URL.String()}) } } @@ -220,11 +223,21 @@ func mainRm(ctx *cli.Context) { rmAll(url, isIncomplete) if removeTopFolder { // Remove top folder as well. - rm(url, isIncomplete) + err := rm(url, isIncomplete) + if err != nil { + errorIf(err.Trace(url), "Unable to remove ‘"+url+"’.") + continue + } + printMsg(rmMessage{url}) } } else { - rm(url, isIncomplete) + err := rm(url, isIncomplete) + if err != nil { + errorIf(err.Trace(url), "Unable to remove ‘"+url+"’.") + continue + } + printMsg(rmMessage{url}) } } } diff --git a/share-download-main.go b/share-download-main.go index 77c173c7..d94c5466 100644 --- a/share-download-main.go +++ b/share-download-main.go @@ -103,7 +103,8 @@ func doShareDownloadURL(targetURL string, recursive bool, expiry time.Duration) // Load previously saved upload-shares. Add new entries and write it back. shareDB := newShareDBV1() - err = shareDB.Load(getShareDownloadsFile()) + shareDownloadsFile := getShareDownloadsFile() + err = shareDB.Load(shareDownloadsFile) if err != nil { return err.Trace() } @@ -114,8 +115,11 @@ func doShareDownloadURL(targetURL string, recursive bool, expiry time.Duration) if content.Err != nil { return content.Err.Trace(clnt.GetURL().String()) } - - objectURL := content.Content.URL.String() + // if any incoming directories, we don't need to calculate. + if content.Type.IsDir() { + continue + } + objectURL := content.URL.String() newClnt, err := url2Client(objectURL) if err != nil { return err.Trace(objectURL) @@ -140,7 +144,7 @@ func doShareDownloadURL(targetURL string, recursive bool, expiry time.Duration) } // Save downloads and return. - return shareDB.Save(getShareDownloadsFile()) + return shareDB.Save(shareDownloadsFile) } // main for share download. @@ -171,6 +175,6 @@ func mainShareDownload(ctx *cli.Context) { targetURL := getAliasURL(url, config.Aliases) // Expand alias. err := doShareDownloadURL(targetURL, isRecursive, expiry) - fatalIf(err.Trace(targetURL), "Unable to share target ‘"+arg+"’.") + errorIf(err.Trace(targetURL), "Unable to share target ‘"+arg+"’.") } } diff --git a/url.go b/url.go index 8e642d2e..5db2eb9c 100644 --- a/url.go +++ b/url.go @@ -93,7 +93,7 @@ func url2DirContent(urlStr string) (content *client.Content, err *probe.Error) { if entry.Err != nil { return nil, entry.Err.Trace(urlStr) } - if strings.HasPrefix(entry.Content.URL.Path, clnt.GetURL().Path) { + if strings.HasPrefix(entry.URL.Path, clnt.GetURL().Path) { content := new(client.Content) content.URL = clnt.GetURL() content.Type = os.ModeDir