diff --git a/cmd-common.go b/cmd-common.go deleted file mode 100644 index 2fe8fd26..00000000 --- a/cmd-common.go +++ /dev/null @@ -1,62 +0,0 @@ -/* - * Minio Client (C) 2014, 2015 Minio, Inc. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package main - -import ( - "net" - - "github.com/minio/minio/pkg/iodine" -) - -// isValidRetry - check if we should retry for the given error sequence -func isValidRetry(err error) bool { - err = iodine.New(err, nil) - if err == nil { - return false - } - // DNSError, Network Operation error - switch e := iodine.ToError(err).(type) { - case *net.AddrError: - return true - case *net.DNSError: - return true - case *net.OpError: - switch e.Op { - case "read", "write", "dial": - return true - } - } - return false -} - -/* -// StartBar -- instantiate a progressbar -func startBar(size int64) *pb.ProgressBar { - bar := pb.New64(size) - bar.SetUnits(pb.U_BYTES) - bar.SetRefreshRate(time.Millisecond * 10) - bar.NotPrint = true - bar.ShowSpeed = true - bar.Callback = func(s string) { - // Colorize - console.Print("\r" + s) - } - // Feels like wget - bar.Format("[=> ]") - return bar -} -*/ diff --git a/cmd-cp.go b/cmd-cp.go index c9bafaac..4fc3c434 100644 --- a/cmd-cp.go +++ b/cmd-cp.go @@ -37,7 +37,7 @@ func doCopy(sourceURL string, sourceConfig *hostConfig, targetURL string, target if !globalQuietFlag { bar.ErrorGet(int64(length)) } - return iodine.New(err, nil) + return iodine.New(err, map[string]string{"URL": sourceURL}) } defer reader.Close() diff --git a/common-methods.go b/common-methods.go index e509bc54..4567eda2 100644 --- a/common-methods.go +++ b/common-methods.go @@ -18,6 +18,7 @@ package main import ( "io" + "net" "os" "runtime" @@ -27,7 +28,7 @@ import ( "github.com/minio/minio/pkg/iodine" ) -// getSource - +// getSource gets a reader from URL< func getSource(sourceURL string, sourceConfig *hostConfig) (reader io.ReadCloser, length uint64, err error) { sourceClnt, err := getNewClient(sourceURL, sourceConfig) if err != nil { @@ -36,13 +37,13 @@ func getSource(sourceURL string, sourceConfig *hostConfig) (reader io.ReadCloser return sourceClnt.GetObject(0, 0) } -// putTarget - -func putTarget(targetURL string, targetConfig *hostConfig, length uint64, data io.Reader) error { +// putTarget writes to URL from reader. +func putTarget(targetURL string, targetConfig *hostConfig, length uint64, reader io.Reader) error { targetClnt, err := getNewClient(targetURL, targetConfig) if err != nil { return iodine.New(err, nil) } - err = targetClnt.PutObject(length, data) + err = targetClnt.PutObject(length, reader) if err != nil { return iodine.New(err, map[string]string{"failedURL": targetURL}) } @@ -94,3 +95,70 @@ func url2Stat(urlStr string) (client client.Client, content *client.Content, err return client, content, nil } + +// source2Client returns client and hostconfig objects from the source URL. +func source2Client(sourceURL string) (client.Client, error) { + // Empty source arg? + sourceURLParse, err := client.Parse(sourceURL) + if err != nil { + return nil, iodine.New(errInvalidSource{URL: sourceURL}, nil) + } + + if sourceURLParse.Path == "" { + return nil, iodine.New(errInvalidSource{URL: sourceURL}, nil) + } + + sourceConfig, err := getHostConfig(sourceURL) + if err != nil { + return nil, iodine.New(errInvalidSource{URL: sourceURL}, nil) + } + + sourceClient, err := getNewClient(sourceURL, sourceConfig) + if err != nil { + return nil, iodine.New(errInvalidSource{URL: sourceURL}, nil) + } + return sourceClient, nil +} + +// target2Client returns client and hostconfig objects from the target URL. +func target2Client(targetURL string) (client.Client, error) { + // Empty target arg? + targetURLParse, err := client.Parse(targetURL) + if err != nil { + return nil, iodine.New(errInvalidTarget{URL: targetURL}, nil) + } + if targetURLParse.Path == "" { + return nil, iodine.New(errInvalidTarget{URL: targetURL}, nil) + } + targetConfig, err := getHostConfig(targetURL) + if err != nil { + return nil, iodine.New(errInvalidTarget{URL: targetURL}, nil) + } + + targetClient, err := getNewClient(targetURL, targetConfig) + if err != nil { + return nil, iodine.New(errInvalidTarget{URL: targetURL}, nil) + } + return targetClient, nil +} + +// isValidRetry - check if we should retry for the given error sequence +func isValidRetry(err error) bool { + err = iodine.New(err, nil) + if err == nil { + return false + } + // DNSError, Network Operation error + switch e := iodine.ToError(err).(type) { + case *net.AddrError: + return true + case *net.DNSError: + return true + case *net.OpError: + switch e.Op { + case "read", "write", "dial": + return true + } + } + return false +} diff --git a/cp-url.go b/cp-url.go index 456c931c..666142bb 100644 --- a/cp-url.go +++ b/cp-url.go @@ -65,52 +65,6 @@ const ( cpURLsTypeD ) -// source2Client returns client and hostconfig objects from the source URL. -func source2Client(sourceURL string) (client.Client, error) { - // Empty source arg? - sourceURLParse, err := client.Parse(sourceURL) - if err != nil { - return nil, iodine.New(errInvalidSource{path: sourceURL}, nil) - } - - if sourceURLParse.Path == "" { - return nil, iodine.New(errInvalidSource{path: sourceURL}, nil) - } - - sourceConfig, err := getHostConfig(sourceURL) - if err != nil { - return nil, iodine.New(errInvalidSource{path: sourceURL}, nil) - } - - sourceClient, err := getNewClient(sourceURL, sourceConfig) - if err != nil { - return nil, iodine.New(errInvalidSource{path: sourceURL}, nil) - } - return sourceClient, nil -} - -// target2Client returns client and hostconfig objects from the target URL. -func target2Client(targetURL string) (client.Client, error) { - // Empty target arg? - targetURLParse, err := client.Parse(targetURL) - if err != nil { - return nil, iodine.New(errInvalidTarget{path: targetURL}, nil) - } - if targetURLParse.Path == "" { - return nil, iodine.New(errInvalidTarget{path: targetURL}, nil) - } - targetConfig, err := getHostConfig(targetURL) - if err != nil { - return nil, iodine.New(errInvalidTarget{path: targetURL}, nil) - } - - targetClient, err := getNewClient(targetURL, targetConfig) - if err != nil { - return nil, iodine.New(errInvalidTarget{path: targetURL}, nil) - } - return targetClient, nil -} - // Check if the target URL represents directory. It may or may not exist yet. func isTargetURLDir(targetURL string) bool { if strings.HasSuffix(targetURL, string(filepath.Separator)) { @@ -178,7 +132,7 @@ func prepareCopyURLsTypeA(sourceURL string, targetURL string) *cpURLs { } if !sourceContent.Type.IsRegular() { // Source is not a regular file - return &cpURLs{Error: iodine.New(errInvalidSource{path: sourceURL}, nil)} + return &cpURLs{Error: iodine.New(errInvalidSource{URL: sourceURL}, nil)} } targetClient, err := target2Client(targetURL) if err != nil { @@ -189,15 +143,10 @@ func prepareCopyURLsTypeA(sourceURL string, targetURL string) *cpURLs { targetContent, err := targetClient.Stat() if err == nil { // Target exists. if !targetContent.Type.IsRegular() { // Target is not a regular file - return &cpURLs{Error: iodine.New(errInvalidTarget{path: targetURL}, nil)} - } - // if target is same Name, Size and Type as source return error and skip - u, _ := client.Parse(targetContent.Name) - if (targetContent.Size == sourceContent.Size) && (filepath.Base(u.Path) == sourceContent.Name) { - // Target exists, don't overwrite. Let the copy function decide return error here - return &cpURLs{Error: iodine.New(errSameURLs{source: sourceURL, target: targetURL}, nil)} + return &cpURLs{Error: iodine.New(errInvalidTarget{URL: targetURL}, nil)} } } + // All OK.. We can proceed. Type A return &cpURLs{SourceContent: sourceContent, TargetContent: &client.Content{Name: targetURL}} } @@ -218,7 +167,7 @@ func prepareCopyURLsTypeB(sourceURL string, targetURL string) *cpURLs { if !sourceContent.Type.IsRegular() { // Source is not a regular file. - return &cpURLs{Error: iodine.New(errInvalidSource{path: sourceURL}, nil)} + return &cpURLs{Error: iodine.New(errInvalidSource{URL: sourceURL}, nil)} } targetClient, err := target2Client(targetURL) @@ -238,12 +187,12 @@ func prepareCopyURLsTypeB(sourceURL string, targetURL string) *cpURLs { // 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 { - return &cpURLs{Error: iodine.New(errInvalidSource{path: sourceURL}, nil)} + return &cpURLs{Error: iodine.New(errInvalidSource{URL: sourceURL}, nil)} } targetURLParse, err := client.Parse(targetURL) if err != nil { - return &cpURLs{Error: iodine.New(errInvalidTarget{path: targetURL}, nil)} + return &cpURLs{Error: iodine.New(errInvalidTarget{URL: targetURL}, nil)} } targetURLParse.Path = filepath.Join(targetURLParse.Path, filepath.Base(sourceURLParse.Path)) @@ -318,19 +267,19 @@ func prepareCopyURLsTypeC(sourceURL, targetURL string) <-chan *cpURLs { // 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 { - cpURLsCh <- &cpURLs{Error: iodine.New(errInvalidSource{path: sourceURL}, nil)} + cpURLsCh <- &cpURLs{Error: iodine.New(errInvalidSource{URL: sourceURL}, nil)} continue } targetURLParse, err := client.Parse(targetURL) if err != nil { - cpURLsCh <- &cpURLs{Error: iodine.New(errInvalidTarget{path: targetURL}, nil)} + cpURLsCh <- &cpURLs{Error: iodine.New(errInvalidTarget{URL: targetURL}, nil)} continue } sourceContentParse, err := client.Parse(sourceContent.Content.Name) if err != nil { - cpURLsCh <- &cpURLs{Error: iodine.New(errInvalidSource{path: sourceContent.Content.Name}, nil)} + cpURLsCh <- &cpURLs{Error: iodine.New(errInvalidSource{URL: sourceContent.Content.Name}, nil)} continue } diff --git a/errors.go b/errors.go index 4556c40c..2a1c7503 100644 --- a/errors.go +++ b/errors.go @@ -93,11 +93,11 @@ func (e errAliasExists) Error() string { /* type errIsNotBucket struct { - path string + URL string } func (e errIsNotBucket) Error() string { - return "Not a bucket " + e.path + return "Not a bucket " + e.URL } // errInvalidAuthKeys - invalid authorization keys @@ -110,26 +110,17 @@ func (e errInvalidAuthKeys) Error() string { */ type errInvalidSource struct { - path string + URL string } func (e errInvalidSource) Error() string { - return "Invalid source " + e.path + return "Invalid source " + e.URL } type errInvalidTarget struct { - path string + URL string } func (e errInvalidTarget) Error() string { - return "Invalid target " + e.path -} - -type errSameURLs struct { - source string - target string -} - -func (e errSameURLs) Error() string { - return "Source " + e.source + " and Target " + e.target + " are same" + return "Invalid target " + e.URL }