diff --git a/cmd/cp-url-syntax.go b/cmd/cp-url-syntax.go index 5432e274..8fb2803d 100644 --- a/cmd/cp-url-syntax.go +++ b/cmd/cp-url-syntax.go @@ -118,24 +118,38 @@ func checkCopySyntaxTypeC(srcURLs []string, tgtURL string, isRecursive bool, key fatalIf(errInvalidArgument().Trace(), "Invalid number of source arguments.") } - srcURL := srcURLs[0] - _, srcContent, err := url2Stat(srcURL, false, keys) - // incomplete uploads are not necessary for copy operation, no need to verify for them. - isIncomplete := false - if err != nil && !isURLPrefixExists(srcURL, isIncomplete) { - fatalIf(err.Trace(srcURL), "Unable to stat source `"+srcURL+"`.") - } - - if srcContent.Type.IsDir() && !isRecursive { - fatalIf(errInvalidArgument().Trace(srcURL), "To copy a folder requires --recursive flag.") - } - // Check target. if _, tgtContent, err := url2Stat(tgtURL, false, keys); err == nil { if !tgtContent.Type.IsDir() { fatalIf(errInvalidArgument().Trace(tgtURL), "Target `"+tgtURL+"` is not a folder.") } } + + for _, srcURL := range srcURLs { + c, srcContent, err := url2Stat(srcURL, false, keys) + // incomplete uploads are not necessary for copy operation, no need to verify for them. + isIncomplete := false + if err != nil { + if !isURLPrefixExists(srcURL, isIncomplete) { + fatalIf(err.Trace(srcURL), "Unable to stat source `"+srcURL+"`.") + } + // No more check here, continue to the next source url + continue + } + + if srcContent.Type.IsDir() { + // Require --recursive flag if we are copying a directory + if !isRecursive { + fatalIf(errInvalidArgument().Trace(srcURL), "To copy a folder requires --recursive flag.") + } + + // Check if we are going to copy a directory into itself + if isURLContains(srcURL, tgtURL, string(c.GetURL().Separator)) { + fatalIf(errInvalidArgument().Trace(), "Copying a folder into itself is not allowed.") + } + } + } + } // checkCopySyntaxTypeD verifies if the source is a valid list of files and target is a valid folder. diff --git a/cmd/mirror-url.go b/cmd/mirror-url.go index d84b4d3a..de6b1c97 100644 --- a/cmd/mirror-url.go +++ b/cmd/mirror-url.go @@ -47,31 +47,35 @@ func checkMirrorSyntax(ctx *cli.Context, encKeyDB map[string][]prefixSSEPair) { errorIf(errInvalidArgument().Trace(URLs...), "`--force` is deprecated please use `--overwrite` instead for the same functionality.") } + tgtClientURL := newClientURL(tgtURL) + if tgtClientURL.Host != "" { + if tgtClientURL.Path == string(tgtClientURL.Separator) { + fatalIf(errInvalidArgument().Trace(tgtURL), + fmt.Sprintf("Target `%s` does not contain bucket name.", tgtURL)) + } + } + /****** Generic rules *******/ if !ctx.Bool("watch") { - _, srcContent, err := url2Stat(srcURL, false, encKeyDB) + c, srcContent, err := url2Stat(srcURL, false, encKeyDB) // incomplete uploads are not necessary for copy operation, no need to verify for them. isIncomplete := false if err != nil && !isURLPrefixExists(srcURL, isIncomplete) { errorIf(err.Trace(srcURL), "Unable to stat source `"+srcURL+"`.") } - if err == nil && !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 command.", srcURL)) + if err == nil { + 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 command.", srcURL)) + } + + // Disallow mirroring a directory to itself + if isURLContains(srcURL, tgtURL, string(c.GetURL().Separator)) { + fatalIf(errInvalidArgument().Trace(), "Mirroring a folder into itself is not allowed.") + } } } - if len(tgtURL) == 0 && tgtURL == "" { - fatalIf(errInvalidArgument().Trace(), "Invalid target arguments to mirror command.") - } - - clientURL := newClientURL(tgtURL) - if clientURL.Host != "" { - if clientURL.Path == string(clientURL.Separator) { - fatalIf(errInvalidArgument().Trace(tgtURL), - fmt.Sprintf("Target `%s` does not contain bucket name.", tgtURL)) - } - } } func matchExcludeOptions(excludeOptions []string, srcSuffix string) bool { diff --git a/cmd/utils.go b/cmd/utils.go index f4c34db0..a62175af 100644 --- a/cmd/utils.go +++ b/cmd/utils.go @@ -272,3 +272,20 @@ func getSSEKey(resource string, encKeys []prefixSSEPair) string { } return "" } + +// Return true if target url is a part of a source url such as: +// alias/bucket/ and alias/bucket/dir/, however +func isURLContains(srcURL, tgtURL, sep string) bool { + // Add a separator to source url if not found + if !strings.HasSuffix(srcURL, sep) { + srcURL += sep + } + if !strings.HasSuffix(tgtURL, sep) { + tgtURL += sep + } + // Check if we are going to copy a directory into itself + if strings.HasPrefix(tgtURL, srcURL) { + return true + } + return false +}