Removing duplication in doCopyCmdRecursive, now depends on doCopyCmd

This commit is contained in:
Frederick F. Kautz IV
2015-04-21 16:03:40 -07:00
parent 8813ecd24d
commit 3ae23a9a85
2 changed files with 27 additions and 162 deletions
+22 -142
View File
@@ -17,161 +17,41 @@
package main
import (
"io"
"runtime"
"strings"
"fmt"
"github.com/cheggaaa/pb"
"github.com/minio-io/mc/pkg/client"
"github.com/minio-io/mc/pkg/console"
"github.com/minio-io/minio/pkg/iodine"
"github.com/minio-io/minio/pkg/utils/log"
)
const (
pathSeparator = "/"
pathSeparatorWindows = "\\"
pathSeparator = "/"
// pathSeparatorWindows = "\\"
)
func getSourceURL(sourceURL, objectName string) string {
if client.GetType(sourceURL) == client.Filesystem && runtime.GOOS == "windows" {
return strings.TrimSuffix(sourceURL, pathSeparator) + pathSeparatorWindows + objectName
}
return strings.TrimSuffix(sourceURL, pathSeparator) + pathSeparator + objectName
}
// getSourceObjectURLMap - get list of all source object and its URLs in a map
func getSourceObjectURLMap(manager clientManager, sourceURL string, sourceConfig *hostConfig) (sourceObjectURLMap map[string]string, err error) {
sourceClnt, err := manager.getNewClient(sourceURL, sourceConfig, globalDebugFlag)
if err != nil {
return nil, iodine.New(err, nil)
}
objects, err := sourceClnt.List()
if err != nil {
return nil, iodine.New(err, nil)
}
sourceObjectURLMap = make(map[string]string)
for _, object := range objects {
sourceObjectURLMap[object.Name] = getSourceURL(sourceURL, object.Name)
}
return sourceObjectURLMap, nil
}
// getRecursiveTargetWriter - recursively get target writers to initiate copying data
func getRecursiveTargetWriter(manager clientManager, targetURL string, targetConfig *hostConfig, md5Hex string, length int64) (io.WriteCloser, error) {
targetClnt, err := manager.getNewClient(targetURL, targetConfig, globalDebugFlag)
if err != nil {
return nil, iodine.New(err, map[string]string{"failedURL": targetURL})
}
// For object storage URL's do a StatBucket() and PutBucket(), not necessary for fs client
if client.GetType(targetURL) != client.Filesystem {
// check if bucket is valid, if not found create it
if err := targetClnt.Stat(); err != nil {
switch iodine.ToError(err).(type) {
case client.BucketNotFound:
err := targetClnt.PutBucket()
if err != nil {
return nil, iodine.New(err, map[string]string{"failedURL": targetURL})
}
default:
return nil, iodine.New(err, map[string]string{"failedURL": targetURL})
}
}
}
return targetClnt.Put(md5Hex, length)
}
// getRecursiveTargetWriters - convenient wrapper around getRecursiveTarget
func getRecursiveTargetWriters(manager clientManager, targetURLConfigMap map[string]*hostConfig, md5Hex string, length int64) ([]io.WriteCloser, error) {
var targetWriters []io.WriteCloser
for targetURL, targetConfig := range targetURLConfigMap {
writer, err := getRecursiveTargetWriter(manager, targetURL, targetConfig, md5Hex, length)
if err != nil {
// on error close all writers
for _, targetWriter := range targetWriters {
targetWriter.Close()
}
// return error so that Multiwriter doesn't get control
return nil, iodine.New(err, nil)
}
targetWriters = append(targetWriters, writer)
}
return targetWriters, nil
}
// doCopyCmdRecursive - copy bucket to bucket
func doCopyCmdRecursive(manager clientManager, sourceURLConfigMap map[string]*hostConfig, targetURLConfigMap map[string]*hostConfig) (string, error) {
for sourceURL, sourceConfig := range sourceURLConfigMap {
sourceObjectURLMap, err := getSourceObjectURLMap(manager, sourceURL, sourceConfig)
if err != nil {
msg := fmt.Sprintf("Getting list of objects from source URL: [%s] failed", sourceURL)
return msg, iodine.New(err, nil)
}
// do not exit, continue even for failures
for sourceObjectName, sourceObjectURL := range sourceObjectURLMap {
sourceClnt, err := manager.getNewClient(sourceObjectURL, sourceConfig, globalDebugFlag)
if err != nil {
err := iodine.New(err, nil)
msg := fmt.Sprintf("Instantiating a new client for URL [%s] failed", sourceObjectURL)
return msg, err
}
reader, length, md5hex, err := sourceClnt.Get()
if err != nil {
err = iodine.New(err, nil)
msg := fmt.Sprintf("Reading from source URL: [%s] failed", sourceURL)
return msg, err
}
newTargetURLConfigMap := make(map[string]*hostConfig)
// Construct full target URL path based on source object name
// get source list
clnt, err := manager.getNewClient(sourceURL, sourceConfig, false)
log.Debug.Println(iodine.New(err, nil))
items, err := clnt.List()
log.Debug.Println(iodine.New(err, nil))
for _, item := range items {
// get source url
sourceURLs := make(map[string]*hostConfig)
sourceObjectURL := strings.TrimSuffix(sourceURL, pathSeparator) + pathSeparator + item.Name
sourceURLs[sourceObjectURL] = sourceConfig
// get target urls
targetURLs := make(map[string]*hostConfig)
for targetURL, targetConfig := range targetURLConfigMap {
targetURL := strings.TrimSuffix(targetURL, pathSeparator) + pathSeparator + sourceObjectName
newTargetURLConfigMap[targetURL] = targetConfig
}
writeClosers, err := getRecursiveTargetWriters(manager, newTargetURLConfigMap, md5hex, length)
if err != nil {
return "Writing to target URLs failed", iodine.New(err, nil)
}
var writers []io.Writer
for _, writer := range writeClosers {
writers = append(writers, writer)
}
// set up progress bar
var bar *pb.ProgressBar
if !globalQuietFlag {
bar = startBar(length)
bar.Start()
writers = append(writers, bar)
}
// write progress bar
multiWriter := io.MultiWriter(writers...)
// copy data to writers
_, err = io.CopyN(multiWriter, reader, length)
if err != nil {
err := iodine.New(err, nil)
return "Copying data from source to target(s) failed", err
}
// close writers
for _, writer := range writeClosers {
err := writer.Close()
if err != nil {
err := iodine.New(err, nil)
return "Connections still active, one or more writes have failed", err
}
}
if !globalDebugFlag {
bar.Finish()
console.Infoln("Success!")
targetObjectURL := strings.TrimSuffix(targetURL, pathSeparator) + pathSeparator + item.Name
targetURLs[targetObjectURL] = targetConfig
}
humanReadable, err := doCopyCmd(manager, sourceURLs, targetURLs)
// TODO work out how to report errors
log.Debug.Println(humanReadable)
log.Debug.Println(iodine.New(err, nil))
}
}
return "", nil
+5 -20
View File
@@ -21,7 +21,6 @@ import (
"encoding/base64"
"encoding/hex"
"io"
"io/ioutil"
"sync"
"time"
@@ -31,6 +30,7 @@ import (
. "github.com/minio-io/check"
"github.com/minio-io/mc/pkg/client"
clientMocks "github.com/minio-io/mc/pkg/client/mocks"
"io/ioutil"
)
type CmdTestSuite struct{}
@@ -97,10 +97,6 @@ func (s *CmdTestSuite) TestCopyRecursive(c *C) {
manager := &MockclientManager{}
cl1 := &clientMocks.Client{}
cl2 := &clientMocks.Client{}
cl3 := &clientMocks.Client{}
cl4 := &clientMocks.Client{}
cl5 := &clientMocks.Client{}
wg := &sync.WaitGroup{}
@@ -151,17 +147,10 @@ func (s *CmdTestSuite) TestCopyRecursive(c *C) {
manager.On("getNewClient", sourceURL, sourceConfig, false).Return(cl1, nil).Once()
cl1.On("List").Return(items, nil).Once()
manager.On("getNewClient", sourceURL+"hello1", sourceConfig, false).Return(cl2, nil).Once()
cl2.On("Get").Return(ioutil.NopCloser(bytes.NewBufferString(data1)), dataLen1, etag1, nil).Once()
manager.On("getNewClient", sourceURL+"hello2", sourceConfig, false).Return(cl3, nil).Once()
cl3.On("Get").Return(ioutil.NopCloser(bytes.NewBufferString(data2)), dataLen2, etag2, nil).Once()
manager.On("getNewClient", targetURL+"hello1", targetConfig, false).Return(cl4, nil).Once()
cl4.On("Stat").Return(nil).Once()
cl4.On("Put", etag1, dataLen1).Return(writer1, nil).Once()
manager.On("getNewClient", targetURL+"hello2", targetConfig, false).Return(cl5, nil).Once()
cl5.On("Stat").Return(nil).Once()
cl5.On("Put", etag2, dataLen2).Return(writer2, nil).Once()
manager.On("getSourceReader", sourceURL+"hello1", sourceConfig).Return(ioutil.NopCloser(bytes.NewBufferString(data1)), dataLen1, etag1, nil).Once()
manager.On("getTargetWriter", targetURL+"hello1", targetConfig, etag1, dataLen1).Return(writer1, nil).Once()
manager.On("getSourceReader", sourceURL+"hello2", sourceConfig).Return(ioutil.NopCloser(bytes.NewBufferString(data2)), dataLen2, etag2, nil).Once()
manager.On("getTargetWriter", targetURL+"hello2", targetConfig, etag2, dataLen2).Return(writer2, nil).Once()
msg, err := doCopyCmdRecursive(manager, sourceURLConfigMap, targetURLConfigMap)
c.Assert(msg, Equals, "")
@@ -175,10 +164,6 @@ func (s *CmdTestSuite) TestCopyRecursive(c *C) {
manager.AssertExpectations(c)
cl1.AssertExpectations(c)
cl2.AssertExpectations(c)
cl3.AssertExpectations(c)
cl4.AssertExpectations(c)
cl5.AssertExpectations(c)
}
func (s *CmdTestSuite) TestCopyCmdFailures(c *C) {