diff --git a/cast-url.go b/cast-url.go index 2625617f..9959fcb9 100644 --- a/cast-url.go +++ b/cast-url.go @@ -21,7 +21,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" ) @@ -47,13 +49,78 @@ const ( // ========================= // A: cast(f, []f) -> cast(f, []f) // B: cast(f, [](d | f)) -> cast(f, [](d/f | f)) -> A: -// C: cast(d1..., [](d2 | f)) -> []cast(d1/f, [](d1/d2/f | d1/f)) -> []A: -// -// * CAST ARGS - INVALID CASES -// =========================== -// cast(d, *) -// cast(d..., f) -// cast(*, d...) +// C: cast(d1..., [](d2)) -> []cast(d1/f, [](d1/d2/f)) -> []A: + +// checkCastSyntax(URLs []string) +func checkCastSyntax(ctx *cli.Context) { + if len(ctx.Args()) < 2 || ctx.Args().First() == "help" { + cli.ShowCommandHelpAndExit(ctx, "cast", 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(), err) + } + + srcURL := URLs[0] + tgtURLs := URLs[1:] + + if !isURLRecursive(srcURL) { + _, srcContent, err := url2Stat(srcURL) + // Source exist?. + if err != nil { + console.Fatalf("Unable to stat source ‘%s’. %s\n", srcURL, iodine.New(err, nil)) + } + + // Rule A & B validation. + if srcContent.Type.IsRegular() { + // 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) + } + } + } + } else { // Source is not a file. + if srcContent.Type.IsDir() { + console.Fatalf("Source ‘%s’ is a directory. Please use ‘%s...’ ellipses sufix to copy a directory and its contents recursively.\n", srcURL, srcURL) + } + console.Fatalf("Source ‘%s’ is not a regular file.\n", srcURL) + } + + } else { // Rule C validation. + 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() { // Elipses is supported only for directories. + console.Fatalf("Source ‘%s’ is not a directory. %s\n", stripRecursiveURL(srcURL), iodine.New(err, nil)) + } + + // 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 directory ‘%s’. %s\n", tgtURL, iodine.New(err, nil)) + } + if !tgtContent.Type.IsDir() { + console.Fatalf("Target ‘%s’ is not a directory.\n", tgtURL) + } + } + } +} // guessCastURLType guesses the type of URL. This approach all allows prepareURL // functions to accurately report failure causes. @@ -179,7 +246,7 @@ func prepareCastURLsTypeB(sourceURL string, targetURLs []string) castURLs { return sURLs } -// prepareCastURLsTypeC - C: cast(f, []d) -> []cast(f, d/f) -> []A +// prepareCastURLsTypeC - C: func prepareCastURLsTypeC(sourceURL string, targetURLs []string) <-chan castURLs { castURLsCh := make(chan castURLs) go func() { diff --git a/cmd-cast.go b/cmd-cast.go index c3d2a8d4..3c9a4824 100644 --- a/cmd-cast.go +++ b/cmd-cast.go @@ -219,9 +219,7 @@ func doCastCmdSession(session *sessionV2) { } func runCastCmd(ctx *cli.Context) { - if len(ctx.Args()) < 2 || ctx.Args().First() == "help" { - cli.ShowCommandHelpAndExit(ctx, "cast", 1) // last argument is exit code - } + checkCastSyntax(ctx) session := newSessionV2() defer session.Close() diff --git a/common-methods.go b/common-methods.go index f6fa1457..9b0e5481 100644 --- a/common-methods.go +++ b/common-methods.go @@ -20,6 +20,7 @@ import ( "io" "os" "runtime" + "strings" "sync" "github.com/minio/mc/pkg/client" @@ -28,6 +29,25 @@ import ( "github.com/minio/minio/pkg/iodine" ) +// Check if the target URL represents directory. It may or may not exist yet. +func isTargetURLDir(targetURL string) bool { + targetURLParse, err := client.Parse(targetURL) + if err != nil { + return false + } + if strings.HasSuffix(targetURLParse.String(), string(targetURLParse.Separator)) { + return true + } + _, targetContent, err := url2Stat(targetURL) + if err != nil { + return false + } + if !targetContent.Type.IsDir() { // Target is a dir. Type B + return false + } + return true +} + // getSource gets a reader from URL< func getSource(sourceURL string) (reader io.ReadCloser, length int64, err error) { sourceClnt, err := source2Client(sourceURL) diff --git a/cp-url.go b/cp-url.go index c896c949..73345a18 100644 --- a/cp-url.go +++ b/cp-url.go @@ -24,29 +24,6 @@ import ( "github.com/minio/minio/pkg/iodine" ) -// -// NOTE: All the parse rules should reduced to A: Copy(Source, Target). -// -// * SINGLE SOURCE - VALID -// ======================= -// A: copy(f, f) -> copy(f, f) -// B: copy(f, d) -> copy(f, d/f) -> A -// C: copy(d1..., d2) -> []copy(d1/f, d1/d2/f) -> []A -// -// * SINGLE SOURCE - INVALID -// ========================= -// copy(d, *) -// copy(d..., f) -// copy(*, d...) -// -// * MULTI-SOURCE RECURSIVE - VALID -// ================================ -// D: copy([](d1... | f), d2) -> []copy(d1/f | f, d2/d1/f | d2/f) -> []A -// -// * MULTI-SOURCE RECURSIVE - INVALID -// ================================== -// copy(*, f) - type copyURLs struct { SourceContent *client.Content TargetContent *client.Content @@ -63,24 +40,20 @@ const ( copyURLsTypeD ) -// Check if the target URL represents directory. It may or may not exist yet. -func isTargetURLDir(targetURL string) bool { - targetURLParse, err := client.Parse(targetURL) - if err != nil { - return false - } - if strings.HasSuffix(targetURLParse.String(), string(targetURLParse.Separator)) { - return true - } - _, targetContent, err := url2Stat(targetURL) - if err != nil { - return false - } - if !targetContent.Type.IsDir() { // Target is a dir. Type B - return false - } - return true -} +// NOTE: All the parse rules should reduced to A: Copy(Source, Target). +// +// * VALID RULES +// ======================= +// A: copy(f, f) -> copy(f, f) +// B: copy(f, d) -> copy(f, d/f) -> A +// C: copy(d1..., d2) -> []copy(d1/f, d1/d2/f) -> []A +// +// * INVALID RULES +// ========================= +// A: copy(d, *) +// B: copy(d..., f) +// C: copy(*, d...) +// // guessCopyURLType guesses the type of URL. This approach all allows prepareURL // functions to accurately report failure causes.