Merge pull request #1296 from harshavardhana/simplify
copy: Simplify copy handling for TypeC
This commit is contained in:
@@ -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 {
|
||||
|
||||
+2
-2
@@ -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 {
|
||||
|
||||
+1
-1
@@ -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),
|
||||
|
||||
+6
-7
@@ -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)
|
||||
}
|
||||
|
||||
@@ -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()
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user