From 56a725ef18d4871bd17f17d75b5ccf9062b08538 Mon Sep 17 00:00:00 2001 From: Harshavardhana Date: Wed, 22 Jul 2015 21:57:25 -0700 Subject: [PATCH] Handle cases where prefix exists or not for both cast and cp --- cast-url.go | 33 +++------------------ cmd-cast.go | 2 +- cmd-cp.go | 5 ++-- common-methods.go | 7 +++-- cp-url.go | 75 ++++++++++++++++++++++++++++++----------------- 5 files changed, 60 insertions(+), 62 deletions(-) diff --git a/cast-url.go b/cast-url.go index 89b5814f..5e44511f 100644 --- a/cast-url.go +++ b/cast-url.go @@ -17,7 +17,6 @@ package main import ( - "os" "path/filepath" "strings" @@ -90,31 +89,9 @@ func checkCastSyntax(ctx *cli.Context) { switch guessCastURLType(srcURL, tgtURLs) { case castURLsTypeA: // Source is already a regular file. - // All targets should be a valid file or folder. - for _, tgtURL := range tgtURLs { - _, tgtContent, err := url2Stat(tgtURL) - // Target exist? - if err != nil { - console.Fatalf("Unable to stat target ‘%s’. %s\n", tgtURL, iodine.New(err, nil)) - } - if !tgtContent.Type.IsRegular() { - console.Fatalf("Target ‘%s’ is not a valid file.\n", tgtURL) - } - } + // case castURLsTypeB: // Source is already a regular file. - // All targets should be a valid file or folder. - for _, tgtURL := range tgtURLs { - _, tgtContent, err := url2Stat(tgtURL) - // Target exist? - if err != nil { - console.Fatalf("Unable to stat target ‘%s’. %s\n", tgtURL, iodine.New(err, nil)) - } - if !tgtContent.Type.IsRegular() { - if !tgtContent.Type.IsDir() { - console.Fatalf("Target ‘%s’ is not a valid file or directory.\n", tgtURL) - } - } - } + // case castURLsTypeC: srcURL = stripRecursiveURL(srcURL) _, srcContent, err := url2Stat(srcURL) @@ -126,6 +103,7 @@ func checkCastSyntax(ctx *cli.Context) { if srcContent.Type.IsRegular() { // Ellipses is supported only for directories. console.Fatalf("Source ‘%s’ is not a directory. %s\n", stripRecursiveURL(srcURL), iodine.New(err, nil)) } + default: console.Fatalln("Invalid arguments. Unable to determine how to cast. Please report this issue at https://github.com/minio/mc/issues") } @@ -200,13 +178,10 @@ func prepareSingleCastURLsTypeB(sourceURL string, targetURL string) castURLs { } _, targetContent, err := url2Stat(targetURL) - if os.IsNotExist(iodine.ToError(err)) { + if err != nil { // Source and target are files. Already reduced to Type A. return prepareSingleCastURLsTypeA(sourceURL, targetURL) } - if err != nil { - return castURLs{Error: NewIodine(iodine.New(err, nil))} - } if targetContent.Type.IsRegular() { // File to File // Source and target are files. Already reduced to Type A. diff --git a/cmd-cast.go b/cmd-cast.go index a2fb1f08..c410a09d 100644 --- a/cmd-cast.go +++ b/cmd-cast.go @@ -220,7 +220,7 @@ func doCastCmdSession(session *sessionV2) { if cURLs.Error == nil { session.Header.LastCopied = cURLs.SourceContent.Name } else { - console.Errorf("Failed to cast ‘%s’, %s\n", cURLs.SourceContent.Name, cURLs.Error) + console.Errorf("Failed to cast ‘%s’, %s\n", cURLs.SourceContent.Name, NewIodine(cURLs.Error)) } case <-trapCh: // Receive interrupt notification. session.Save() diff --git a/cmd-cp.go b/cmd-cp.go index b610a36c..7412224f 100644 --- a/cmd-cp.go +++ b/cmd-cp.go @@ -107,6 +107,7 @@ func doCopy(cpURLs copyURLs, bar *barSend, cpQueue chan bool, wg *sync.WaitGroup if !globalQuietFlag || !globalJSONFlag { bar.ErrorPut(length) } + console.Println("") console.Errorln(NewIodine(err)) } return nil @@ -211,9 +212,7 @@ func doCopyCmdSession(session *sessionV2) { // runCopyCmd is bound to sub-command func runCopyCmd(ctx *cli.Context) { - if len(ctx.Args()) < 2 || ctx.Args().First() == "help" { - cli.ShowCommandHelpAndExit(ctx, "cp", 1) // last argument is exit code - } + checkCopySyntax(ctx) session := newSessionV2() defer session.Close() diff --git a/common-methods.go b/common-methods.go index e88061f9..16b499fa 100644 --- a/common-methods.go +++ b/common-methods.go @@ -35,14 +35,17 @@ func isTargetURLDir(targetURL string) bool { if err != nil { return false } - if strings.HasSuffix(targetURLParse.String(), string(targetURLParse.Separator)) { + if targetURLParse.Path == string(targetURLParse.Separator) && targetURLParse.Scheme != "" { + return false + } + if strings.HasSuffix(targetURLParse.Path, string(targetURLParse.Separator)) { return true } _, targetContent, err := url2Stat(targetURL) if err != nil { return false } - if !targetContent.Type.IsDir() { // Target is a dir. Type B + if !targetContent.Type.IsDir() { // Target is a dir. return false } return true diff --git a/cp-url.go b/cp-url.go index e9160f82..f84c6696 100644 --- a/cp-url.go +++ b/cp-url.go @@ -20,7 +20,9 @@ import ( "path/filepath" "strings" + "github.com/minio/cli" "github.com/minio/mc/pkg/client" + "github.com/minio/mc/pkg/console" "github.com/minio/minio/pkg/iodine" ) @@ -54,6 +56,52 @@ const ( // B: copy(d..., f) // C: copy(*, d...) // +func checkCopySyntax(ctx *cli.Context) { + if len(ctx.Args()) < 2 || ctx.Args().First() == "help" { + cli.ShowCommandHelpAndExit(ctx, "cp", 1) // last argument is exit code. + } + + // extract URLs. + URLs, err := args2URLs(ctx.Args()) + if err != nil { + console.Fatalf("One or more unknown URL types found %s. %s\n", ctx.Args(), iodine.New(err, nil)) + } + + srcURLs := URLs[:len(URLs)-1] + tgtURL := URLs[len(URLs)-1] + + /****** Generic rules *******/ + // Recursive URLs are not allowed in target. + if isURLRecursive(tgtURL) { + console.Fatalf("Target ‘%s’ cannot be recursive. %s\n", tgtURL, iodine.New(err, nil)) + } + + switch guessCopyURLType(srcURLs, tgtURL) { + case copyURLsTypeA: // Source is already a regular file. + // no verification needed, pass through + case copyURLsTypeB: // Source is already a regular file. + // no verification needed, pass through + case copyURLsTypeC: + for _, srcURL := range srcURLs { + srcURL = stripRecursiveURL(srcURL) + _, srcContent, err := url2Stat(srcURL) + // Source exist?. + if err != nil { + console.Fatalf("Unable to stat source ‘%s’. %s\n", srcURL, iodine.New(err, nil)) + } + if srcContent.Type.IsRegular() { // Ellipses is supported only for directories. + console.Fatalf("Source ‘%s’ is not a directory. %s\n", stripRecursiveURL(srcURL), iodine.New(err, nil)) + } + } + case copyURLsTypeD: + // only verify if target is a valid directory and exists + if !isTargetURLDir(tgtURL) { + console.Fatalf("Target ‘%s’ should be a directory and exist, when we have a mixture of files and folders in source\n", tgtURL) + } + default: + console.Fatalln("Invalid arguments. Unable to determine how to copy. Please report this issue at https://github.com/minio/mc/issues") + } +} // guessCopyURLType guesses the type of URL. This approach all allows prepareURL // functions to accurately report failure causes. @@ -123,19 +171,6 @@ func prepareCopyURLsTypeB(sourceURL string, targetURL string) <-chan copyURLs { return } - _, targetContent, err := url2Stat(targetURL) - if err != nil { - copyURLsCh <- copyURLs{Error: NewIodine(iodine.New(err, nil))} - return - } - if err == nil { - if !targetContent.Type.IsDir() { - // Target exists, but is not a directory. - copyURLsCh <- copyURLs{Error: NewIodine(iodine.New(errTargetIsNotDir{URL: targetURL}, nil))} - return - } - } // Else name is available to create. - // All OK.. We can proceed. Type B: source is a file, target is a directory and exists. sourceURLParse, err := client.Parse(sourceURL) if err != nil { @@ -244,20 +279,6 @@ func prepareCopyURLsTypeD(sourceURLs []string, targetURL string) <-chan copyURLs return } - _, targetContent, err := url2Stat(targetURL) - // Target exist? - if err != nil { - // Target does not exist. - copyURLsCh <- copyURLs{Error: NewIodine(iodine.New(errTargetNotFound{URL: targetURL}, nil))} - return - } - - if !targetContent.Type.IsDir() { - // Target exists, but is not a directory. - copyURLsCh <- copyURLs{Error: NewIodine(iodine.New(errTargetIsNotDir{URL: targetURL}, nil))} - return - } - for _, sourceURL := range sourceURLs { // Target is directory. Possibilities are only Type B and C // Is it a recursive URL "..."?