From f41906487154d56562f96c1efcf1866c521aea2d Mon Sep 17 00:00:00 2001 From: Harshavardhana Date: Fri, 13 Nov 2015 15:59:46 -0800 Subject: [PATCH] copy: Simplify copy handling for TypeC --- cp-url.go | 46 ++++++++------------------------------------- mirror-url.go | 4 ++-- pkg/client/fs/fs.go | 2 +- pkg/client/s3/s3.go | 13 ++++++------- typed-errors.go | 3 --- 5 files changed, 17 insertions(+), 51 deletions(-) diff --git a/cp-url.go b/cp-url.go index 7dd279ad..ed86408b 100644 --- a/cp-url.go +++ b/cp-url.go @@ -18,7 +18,6 @@ package main import ( "fmt" - "os" "path/filepath" "strings" @@ -74,6 +73,7 @@ func checkCopySyntax(ctx *cli.Context) { if isURLRecursive(tgtURL) { fatalIf(errDummy().Trace(), fmt.Sprintf("Recursive option is not supported for target ‘%s’ argument.", tgtURL)) } + url := client.NewURL(tgtURL) if url.Host != "" { // This check is for type URL. @@ -81,6 +81,7 @@ func checkCopySyntax(ctx *cli.Context) { fatalIf(errInvalidArgument().Trace(), fmt.Sprintf("Target ‘%s’ does not contain bucket name.", tgtURL)) } } + switch guessCopyURLType(srcURLs, tgtURL) { case copyURLsTypeA: // File -> File. checkCopySyntaxTypeA(srcURLs, tgtURL) @@ -102,7 +103,6 @@ func checkCopySyntaxTypeA(srcURLs []string, tgtURL string) { } srcURL := srcURLs[0] _, srcContent, err := url2Stat(srcURL) - fatalIf(err.Trace(srcURL), "Unable to stat source ‘"+srcURL+"’.") if srcContent.Type.IsDir() { @@ -146,7 +146,6 @@ func checkCopySyntaxTypeC(srcURLs []string, tgtURL string) { srcURL := srcURLs[0] srcURL = stripRecursiveURL(srcURL) - srcContent, err := url2Content(srcURL) fatalIf(err.Trace(srcURL), "Unable to stat source ‘"+srcURL+"’.") @@ -167,15 +166,12 @@ func checkCopySyntaxTypeD(srcURLs []string, tgtURL string) { for _, srcURL := range srcURLs { if isURLRecursive(srcURL) { srcURL = stripRecursiveURL(srcURL) - _, srcContent, err := url2Stat(srcURL) + srcContent, err := url2Content(srcURL) fatalIf(err.Trace(srcURL), "Unable to stat source ‘"+srcURL+"’.") if !srcContent.Type.IsDir() { // Ellipses is supported only for folders. fatalIf(errInvalidArgument().Trace(srcURL), "Source ‘"+srcURL+"’ is not a folder.") } - } else { // Regular URL. - _, _, err := url2Stat(srcURL) - fatalIf(err.Trace(srcURL), "Unable to stat source ‘"+srcURL+"’.") } } _, tgtContent, err := url2Stat(tgtURL) @@ -242,6 +238,7 @@ func prepareCopyURLsTypeB(sourceURL string, targetURL string) copyURLs { // Source does not exist or insufficient privileges. return copyURLs{Error: err.Trace(sourceURL)} } + if !sourceContent.Type.IsRegular() { if sourceContent.Type.IsDir() { return copyURLs{Error: errSourceIsDir(sourceURL).Trace()} @@ -251,12 +248,10 @@ func prepareCopyURLsTypeB(sourceURL string, targetURL string) copyURLs { } // All OK.. We can proceed. Type B: source is a file, target is a folder and exists. - { - sourceURLParse := client.NewURL(sourceURL) - targetURLParse := client.NewURL(targetURL) - targetURLParse.Path = filepath.Join(targetURLParse.Path, filepath.Base(sourceURLParse.Path)) - return prepareCopyURLsTypeA(sourceURL, targetURLParse.String()) - } + sourceURLParse := client.NewURL(sourceURL) + targetURLParse := client.NewURL(targetURL) + targetURLParse.Path = filepath.Join(targetURLParse.Path, filepath.Base(sourceURLParse.Path)) + return prepareCopyURLsTypeA(sourceURL, targetURLParse.String()) } // SINGLE SOURCE - Type C: copy(d1..., d2) -> []copy(d1/f, d1/d2/f) -> []A @@ -279,31 +274,6 @@ func prepareCopyURLsTypeC(sourceURL, targetURL string) <-chan copyURLs { copyURLsCh <- copyURLs{Error: err.Trace(sourceURL)} return } - _, sourceContent, err := url2Stat(sourceURL) - if err != nil { - // if file not found it could be that user has provided a valid - // prefix, populate parent content properly and set error to nil. - // if at all the prefix doesn't exist eventually 'List' will handle - // it properly. - if os.IsNotExist(err.ToGoError()) { - // set the err back to nil consciously. - err = nil - // fill sourceContent with source client data. - sourceContent = new(client.Content) - sourceContent.URL = sourceClient.GetURL() - sourceContent.Type = os.ModeDir - } else { - // Source does not exist or insufficient privileges. - copyURLsCh <- copyURLs{Error: err.Trace(sourceURL)} - return - } - } - - if !sourceContent.Type.IsDir() { - // Source is not a dir. - copyURLsCh <- copyURLs{Error: errSourceIsNotDir(sourceURL).Trace()} - return - } for sourceContent := range sourceClient.List(true, false) { if sourceContent.Err != nil { diff --git a/mirror-url.go b/mirror-url.go index 5397581e..6f44773f 100644 --- a/mirror-url.go +++ b/mirror-url.go @@ -66,11 +66,11 @@ func checkMirrorSyntax(ctx *cli.Context) { fatalIf(err.Trace(srcURL), "Unable to stat source ‘"+newSrcURL+"’.") if !srcContent.Type.IsDir() { - fatalIf(errInvalidArgument().Trace(srcContent.URL.String(), srcContent.Type.String()), fmt.Sprintf("Source ‘%s’ is not a folder. Only folders are supported by mirror.", srcURL)) + fatalIf(errInvalidArgument().Trace(srcContent.URL.String(), srcContent.Type.String()), fmt.Sprintf("Source ‘%s’ is not a folder. Only folders are supported by mirror command.", srcURL)) } if len(tgtURLs) == 0 && tgtURLs == nil { - fatalIf(errInvalidArgument().Trace(), "Invalid number of target arguments to mirror command.") + fatalIf(errInvalidArgument().Trace(), "Invalid target arguments to mirror command.") } for _, tgtURL := range tgtURLs { diff --git a/pkg/client/fs/fs.go b/pkg/client/fs/fs.go index c71c7725..7e09e35b 100644 --- a/pkg/client/fs/fs.go +++ b/pkg/client/fs/fs.go @@ -295,7 +295,7 @@ func (f *fsClient) listInRoutine(contentCh chan client.ContentOnChannel) { } return } - // if os.IsNotExit() fails we return genuine error back to the caller. + // if client.PathNotFound fails we return genuine error back to the caller. contentCh <- client.ContentOnChannel{ Content: nil, Err: err.Trace(fpath), diff --git a/pkg/client/s3/s3.go b/pkg/client/s3/s3.go index cd245b45..34780ab7 100644 --- a/pkg/client/s3/s3.go +++ b/pkg/client/s3/s3.go @@ -230,13 +230,12 @@ func (c *s3Client) Stat() (*client.Content, *probe.Error) { errResponse := minio.ToErrorResponse(err) if errResponse != nil { if errResponse.Code == "NoSuchKey" { - // 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) { + // Append "/" to the object name proactively and see if the Listing + // produces an output. If yes, then we treat it as a directory. + prefixName := object + // Trim any trailing separators and add it. + prefixName = strings.TrimSuffix(prefixName, string(c.hostURL.Separator)) + string(c.hostURL.Separator) + for objectStat := range c.api.ListObjects(bucket, prefixName, false) { if objectStat.Err != nil { return nil, probe.NewError(objectStat.Err) } diff --git a/typed-errors.go b/typed-errors.go index 11f0b415..57cd1ea1 100644 --- a/typed-errors.go +++ b/typed-errors.go @@ -63,9 +63,6 @@ var ( return probe.NewError(errors.New("Source ‘" + URL + "’ is not recursive.")).Untrace() } - errSourceIsNotDir = func(URL string) *probe.Error { - return probe.NewError(errors.New("Source ‘" + URL + "’ is not a folder.")).Untrace() - } errSourceIsDir = func(URL string) *probe.Error { return probe.NewError(errors.New("Source ‘" + URL + "’ is a folder.")).Untrace() }