Disallow copying/mirroring a folder into itself (#2553)

As unix cp command complains when you copy a directory into itself,
(such as `cp -r ~/Desktop/ ~/Desktop/sub/`), mc will follow the
same behavior with this commit.

The main reason is that copy or mirror command can be unpredictable
with the style of copy.

Example:

```
$ mc ls -r myminio/testbucket/
[2018-09-25 19:28:17 CET]     8B dir/object
[2018-09-25 19:28:40 CET]     8B object

$ mc -q cp -r myminio/testbucket/ myminio/testbucket/dir/
`myminio/testbucket/dir/object` -> `myminio/testbucket/dir/dir/object`
`myminio/testbucket/object` -> `myminio/testbucket/dir/object`

```

We can notice that `myminio/testbucket/dir/object` is both source and target
in the copy operation. The last copy operation that will occur will rule the
result of the total copy operation and `myminio/testbucket/dir/dir/object`
will have a different content in each copy operation.

Due to the unpredictability mentionned above, we will disallow copying or
mirroring a folder into itself.
This commit is contained in:
Anis Elleuch
2018-09-25 17:34:40 -07:00
committed by Dee Koder
parent ab31cf97da
commit 87f7e65c4c
3 changed files with 61 additions and 26 deletions
+26 -12
View File
@@ -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.
+18 -14
View File
@@ -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 {
+17
View File
@@ -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
}