Merge pull request #956 from harshavardhana/pr_out_cast_to_mirror

This commit is contained in:
Harshavardhana
2015-07-30 23:11:38 -07:00
5 changed files with 68 additions and 67 deletions
+8 -8
View File
@@ -53,30 +53,30 @@ func (s *CmdTestSuite) TestCopyURLType(c *C) {
c.Assert(guessCopyURLType(sourceURLs, targetURL), Equals, copyURLsTypeInvalid)
}
func (s *CmdTestSuite) TestCastURLType(c *C) {
func (s *CmdTestSuite) TestMirrorURLType(c *C) {
sourceURL := server.URL + "/bucket"
targetURLs := []string{}
c.Assert(guessCastURLType(sourceURL, targetURLs), Equals, mirrorURLsTypeInvalid)
c.Assert(guessMirrorURLType(sourceURL, targetURLs), Equals, mirrorURLsTypeInvalid)
sourceURL = server.URL + "/bucket"
targetURLs = nil
c.Assert(guessCastURLType(sourceURL, targetURLs), Equals, mirrorURLsTypeInvalid)
c.Assert(guessMirrorURLType(sourceURL, targetURLs), Equals, mirrorURLsTypeInvalid)
sourceURL = ""
targetURLs = []string{server.URL + "/bucket/object_new"}
c.Assert(guessCastURLType(sourceURL, targetURLs), Equals, mirrorURLsTypeInvalid)
c.Assert(guessMirrorURLType(sourceURL, targetURLs), Equals, mirrorURLsTypeInvalid)
sourceURL = server.URL + "/bucket..."
targetURLs = []string{server.URL + "/bucket"}
c.Assert(guessCastURLType(sourceURL, targetURLs), Equals, mirrorURLsTypeC)
c.Assert(guessMirrorURLType(sourceURL, targetURLs), Equals, mirrorURLsTypeC)
sourceURL = server.URL + "/bucket/object1"
targetURLs = []string{server.URL + "/bucket"}
c.Assert(guessCastURLType(sourceURL, targetURLs), Equals, mirrorURLsTypeB)
c.Assert(guessMirrorURLType(sourceURL, targetURLs), Equals, mirrorURLsTypeB)
sourceURL = server.URL + "/bucket/object1"
targetURLs = []string{server.URL + "/bucket/object_new"}
c.Assert(guessCastURLType(sourceURL, targetURLs), Equals, mirrorURLsTypeA)
c.Assert(guessMirrorURLType(sourceURL, targetURLs), Equals, mirrorURLsTypeA)
}
// TODO fix both copy and mirror
@@ -92,7 +92,7 @@ func (s *CmdTestSuite) TestCopyContext(c *C) {
console.IsExited = false
}
func (s *CmdTestSuite) TestCastContext(c *C) {
func (s *CmdTestSuite) TestMirrorContext(c *C) {
err := app.Run([]string{os.Args[0], "mirror", server.URL + "/bucket...", server.URL + "/bucket"})
c.Assert(err, IsNil)
c.Assert(console.IsExited, Equals, true)
+21 -21
View File
@@ -35,7 +35,7 @@ import (
var mirrorCmd = cli.Command{
Name: "mirror",
Usage: "Copy files and folders from a single source to many destinations",
Action: runCastCmd,
Action: runMirrorCmd,
CustomHelpTemplate: `NAME:
mc {{.Name}} - {{.Usage}}
@@ -50,23 +50,23 @@ FLAGS:
{{end}}{{ end }}
EXAMPLES:
1. Cast a bucket recursively from Minio cloud storage to multiple buckets on Amazon S3 cloud storage.
1. Mirror a bucket recursively from Minio cloud storage to multiple buckets on Amazon S3 cloud storage.
$ mc {{.Name}} https://play.minio.io:9000/photos/2014... https://s3.amazonaws.com/backup-photos https://s3-west-1.amazonaws.com/local-photos
2. Cast a local folder recursively to Minio cloud storage and Amazon S3 cloud storage.
2. Mirror a local folder recursively to Minio cloud storage and Amazon S3 cloud storage.
$ mc {{.Name}} backup/... https://play.minio.io:9000/archive https://s3.amazonaws.com/archive
3. Cast a bucket from aliased Amazon S3 cloud storage to multiple folders on Windows.
3. Mirror a bucket from aliased Amazon S3 cloud storage to multiple folders on Windows.
$ mc {{.Name}} s3:documents/2014/... C:\backup\2014 C:\shared\volume\backup\2014
4. Cast a local folder of non english character recursively to Amazon s3 cloud storage and Minio cloud storage.
4. Mirror a local folder of non english character recursively to Amazon s3 cloud storage and Minio cloud storage.
$ mc {{.Name}} 本語/... s3:mylocaldocuments play:backup
`,
}
// doCast - Cast an object to multiple destination. mirrorURLs status contains a copy of sURLs and error if any.
func doCast(sURLs mirrorURLs, bar *barSend, mirrorQueueCh <-chan bool, wg *sync.WaitGroup, statusCh chan<- mirrorURLs) {
// doMirror - Mirror an object to multiple destination. mirrorURLs status contains a copy of sURLs and error if any.
func doMirror(sURLs mirrorURLs, bar *barSend, mirrorQueueCh <-chan bool, wg *sync.WaitGroup, statusCh chan<- mirrorURLs) {
defer wg.Done() // Notify that this copy routine is done.
defer func() {
<-mirrorQueueCh
@@ -99,7 +99,7 @@ func doCast(sURLs mirrorURLs, bar *barSend, mirrorQueueCh <-chan bool, wg *sync.
var newReader io.ReadCloser
if globalQuietFlag || globalJSONFlag {
console.PrintC(CastMessage{
console.PrintC(MirrorMessage{
Source: sURLs.SourceContent.Name,
Targets: targetURLs,
})
@@ -124,15 +124,15 @@ func doCast(sURLs mirrorURLs, bar *barSend, mirrorQueueCh <-chan bool, wg *sync.
statusCh <- sURLs
}
// doCastFake - Perform a fake mirror to update the progress bar appropriately.
func doCastFake(sURLs mirrorURLs, bar *barSend) {
// doMirrorFake - Perform a fake mirror to update the progress bar appropriately.
func doMirrorFake(sURLs mirrorURLs, bar *barSend) {
if !globalDebugFlag || !globalJSONFlag {
bar.Progress(sURLs.SourceContent.Size)
}
}
// doPrepareCastURLs scans the source URL and prepares a list of objects for mirroring.
func doPrepareCastURLs(session *sessionV2, trapCh <-chan bool) {
// doPrepareMirrorURLs scans the source URL and prepares a list of objects for mirroring.
func doPrepareMirrorURLs(session *sessionV2, trapCh <-chan bool) {
sourceURL := session.Header.CommandArgs[0] // first one is source.
targetURLs := session.Header.CommandArgs[1:]
var totalBytes int64
@@ -142,7 +142,7 @@ func doPrepareCastURLs(session *sessionV2, trapCh <-chan bool) {
dataFP := session.NewDataWriter()
scanBar := scanBarFactory("")
URLsCh := prepareCastURLs(sourceURL, targetURLs)
URLsCh := prepareMirrorURLs(sourceURL, targetURLs)
done := false
for done == false {
select {
@@ -176,11 +176,11 @@ func doPrepareCastURLs(session *sessionV2, trapCh <-chan bool) {
session.Save()
}
func doCastCmdSession(session *sessionV2) {
func doMirrorCmdSession(session *sessionV2) {
trapCh := signalTrap(os.Interrupt, os.Kill)
if !session.HasData() {
doPrepareCastURLs(session, trapCh)
doPrepareMirrorURLs(session, trapCh)
}
// Set up progress bar.
@@ -203,7 +203,7 @@ func doCastCmdSession(session *sessionV2) {
// Status channel for receiveing mirror return status.
statusCh := make(chan mirrorURLs)
// Go routine to monitor doCast status and signal traps.
// Go routine to monitor doMirror status and signal traps.
wg.Add(1)
go func() {
defer wg.Done()
@@ -238,7 +238,7 @@ func doCastCmdSession(session *sessionV2) {
var sURLs mirrorURLs
json.Unmarshal([]byte(scanner.Text()), &sURLs)
if isCopied(sURLs.SourceContent.Name) {
doCastFake(sURLs, &bar)
doMirrorFake(sURLs, &bar)
} else {
// Wait for other mirror routines to
// complete. We only have limited CPU
@@ -247,7 +247,7 @@ func doCastCmdSession(session *sessionV2) {
// Account for each mirror routines we start.
mirrorWg.Add(1)
// Do mirroring in background concurrently.
go doCast(sURLs, &bar, mirrorQueue, mirrorWg, statusCh)
go doMirror(sURLs, &bar, mirrorQueue, mirrorWg, statusCh)
}
}
mirrorWg.Wait()
@@ -256,8 +256,8 @@ func doCastCmdSession(session *sessionV2) {
wg.Wait()
}
func runCastCmd(ctx *cli.Context) {
checkCastSyntax(ctx)
func runMirrorCmd(ctx *cli.Context) {
checkMirrorSyntax(ctx)
session := newSessionV2()
@@ -278,7 +278,7 @@ func runCastCmd(ctx *cli.Context) {
console.Fatalf("One or more unknown URL types found in %s. %s\n", ctx.Args(), err)
}
doCastCmdSession(session)
doMirrorCmdSession(session)
session.Close()
session.Delete()
}
+35 -34
View File
@@ -41,7 +41,7 @@ const (
mirrorURLsTypeC
)
// NOTE: All the parse rules should reduced to A: Cast(Source, []Target).
// NOTE: All the parse rules should reduced to A: Mirror(Source, []Target).
//
// * CAST ARGS - VALID CASES
// =========================
@@ -49,11 +49,12 @@ const (
// B: mirror(f, [](d | f)) -> mirror(f, [](d/f | f)) -> A:
// C: mirror(d1..., [](d2)) -> []mirror(d1/f, [](d2/d1/f)) -> []A:
// checkCastSyntax(URLs []string)
func checkCastSyntax(ctx *cli.Context) {
// checkMirrorSyntax(URLs []string)
func checkMirrorSyntax(ctx *cli.Context) {
if len(ctx.Args()) < 2 || ctx.Args().First() == "help" {
cli.ShowCommandHelpAndExit(ctx, "mirror", 1) // last argument is exit code.
}
// extract URLs.
URLs, err := args2URLs(ctx.Args())
if err != nil {
@@ -97,19 +98,19 @@ func checkCastSyntax(ctx *cli.Context) {
}
}
switch guessCastURLType(srcURL, tgtURLs) {
switch guessMirrorURLType(srcURL, tgtURLs) {
case mirrorURLsTypeA: // File -> File.
checkCastSyntaxTypeA(srcURL, tgtURLs)
checkMirrorSyntaxTypeA(srcURL, tgtURLs)
case mirrorURLsTypeB: // File -> Folder.
checkCastSyntaxTypeB(srcURL, tgtURLs)
checkMirrorSyntaxTypeB(srcURL, tgtURLs)
case mirrorURLsTypeC: // Folder -> Folder.
checkCastSyntaxTypeC(srcURL, tgtURLs)
checkMirrorSyntaxTypeC(srcURL, tgtURLs)
default:
console.Fatalln("Invalid arguments. Unable to determine how to mirror. Please report this issue at https://github.com/minio/mc/issues")
}
}
func checkCastSyntaxTypeA(srcURL string, tgtURLs []string) {
func checkMirrorSyntaxTypeA(srcURL string, tgtURLs []string) {
if len(tgtURLs) == 0 && tgtURLs == nil {
console.Fatalf("Invalid number of target arguments to mirror command. %s\n", NewIodine(iodine.New(errInvalidArgument{}, nil)))
}
@@ -135,7 +136,7 @@ func checkCastSyntaxTypeA(srcURL string, tgtURLs []string) {
}
}
func checkCastSyntaxTypeB(srcURL string, tgtURLs []string) {
func checkMirrorSyntaxTypeB(srcURL string, tgtURLs []string) {
if len(tgtURLs) == 0 && tgtURLs == nil {
console.Fatalf("Invalid number of target arguments to mirror command. %s\n", NewIodine(iodine.New(errInvalidArgument{}, nil)))
}
@@ -153,7 +154,7 @@ func checkCastSyntaxTypeB(srcURL string, tgtURLs []string) {
// targetURL can be folder or file, internally TypeB calls TypeA if it finds a file
}
func checkCastSyntaxTypeC(srcURL string, tgtURLs []string) {
func checkMirrorSyntaxTypeC(srcURL string, tgtURLs []string) {
if len(tgtURLs) == 0 && tgtURLs == nil {
console.Fatalf("Invalid number of target arguments to mirror command. %s\n", NewIodine(iodine.New(errInvalidArgument{}, nil)))
}
@@ -177,9 +178,9 @@ func checkCastSyntaxTypeC(srcURL string, tgtURLs []string) {
}
}
// guessCastURLType guesses the type of URL. This approach all allows prepareURL
// guessMirrorURLType guesses the type of URL. This approach all allows prepareURL
// functions to accurately report failure causes.
func guessCastURLType(sourceURL string, targetURLs []string) mirrorURLsType {
func guessMirrorURLType(sourceURL string, targetURLs []string) mirrorURLsType {
if targetURLs == nil || len(targetURLs) == 0 { // Target is empty
return mirrorURLsTypeInvalid
}
@@ -203,8 +204,8 @@ func guessCastURLType(sourceURL string, targetURLs []string) mirrorURLsType {
return mirrorURLsTypeA
}
// prepareSingleCastURLTypeA - prepares a single source and single target argument for mirroring.
func prepareSingleCastURLsTypeA(sourceURL string, targetURL string) mirrorURLs {
// prepareSingleMirrorURLTypeA - prepares a single source and single target argument for mirroring.
func prepareSingleMirrorURLsTypeA(sourceURL string, targetURL string) mirrorURLs {
_, sourceContent, err := url2Stat(sourceURL)
if err != nil { // Source does not exist or insufficient privileges.
return mirrorURLs{Error: NewIodine(iodine.New(err, nil))}
@@ -218,11 +219,11 @@ func prepareSingleCastURLsTypeA(sourceURL string, targetURL string) mirrorURLs {
return mirrorURLs{SourceContent: sourceContent, TargetContents: []*client.Content{{Name: targetURL}}}
}
// prepareCastURLsTypeA - A: mirror(f, f) -> mirror(f, f)
func prepareCastURLsTypeA(sourceURL string, targetURLs []string) mirrorURLs {
// prepareMirrorURLsTypeA - A: mirror(f, f) -> mirror(f, f)
func prepareMirrorURLsTypeA(sourceURL string, targetURLs []string) mirrorURLs {
var sURLs mirrorURLs
for _, targetURL := range targetURLs { // Prepare each target separately
URLs := prepareSingleCastURLsTypeA(sourceURL, targetURL)
URLs := prepareSingleMirrorURLsTypeA(sourceURL, targetURL)
if URLs.Error != nil {
return mirrorURLs{Error: NewIodine(iodine.New(URLs.Error, nil))}
}
@@ -232,8 +233,8 @@ func prepareCastURLsTypeA(sourceURL string, targetURLs []string) mirrorURLs {
return sURLs
}
// prepareSingleCastURLsTypeB - prepares a single target and single source URLs for mirroring.
func prepareSingleCastURLsTypeB(sourceURL string, targetURL string) mirrorURLs {
// prepareSingleMirrorURLsTypeB - prepares a single target and single source URLs for mirroring.
func prepareSingleMirrorURLsTypeB(sourceURL string, targetURL string) mirrorURLs {
_, sourceContent, err := url2Stat(sourceURL)
if err != nil {
// Source does not exist or insufficient privileges.
@@ -248,12 +249,12 @@ func prepareSingleCastURLsTypeB(sourceURL string, targetURL string) mirrorURLs {
_, targetContent, err := url2Stat(targetURL)
if err != nil {
// Source and target are files. Already reduced to Type A.
return prepareSingleCastURLsTypeA(sourceURL, targetURL)
return prepareSingleMirrorURLsTypeA(sourceURL, targetURL)
}
if targetContent.Type.IsRegular() { // File to File
// Source and target are files. Already reduced to Type A.
return prepareSingleCastURLsTypeA(sourceURL, targetURL)
return prepareSingleMirrorURLsTypeA(sourceURL, targetURL)
}
// Source is a file, target is a folder and exists.
@@ -268,14 +269,14 @@ func prepareSingleCastURLsTypeB(sourceURL string, targetURL string) mirrorURLs {
}
// Reduce Type B to Type A.
targetURLParse.Path = filepath.Join(targetURLParse.Path, filepath.Base(sourceURLParse.Path))
return prepareSingleCastURLsTypeA(sourceURL, targetURLParse.String())
return prepareSingleMirrorURLsTypeA(sourceURL, targetURLParse.String())
}
// prepareCastURLsTypeB - B: mirror(f, d) -> mirror(f, d/f) -> A
func prepareCastURLsTypeB(sourceURL string, targetURLs []string) mirrorURLs {
// prepareMirrorURLsTypeB - B: mirror(f, d) -> mirror(f, d/f) -> A
func prepareMirrorURLsTypeB(sourceURL string, targetURLs []string) mirrorURLs {
var sURLs mirrorURLs
for _, targetURL := range targetURLs {
URLs := prepareSingleCastURLsTypeB(sourceURL, targetURL)
URLs := prepareSingleMirrorURLsTypeB(sourceURL, targetURL)
if URLs.Error != nil {
return mirrorURLs{Error: NewIodine(iodine.New(URLs.Error, nil))}
}
@@ -285,8 +286,8 @@ func prepareCastURLsTypeB(sourceURL string, targetURLs []string) mirrorURLs {
return sURLs
}
// prepareCastURLsTypeC - C:
func prepareCastURLsTypeC(sourceURL string, targetURLs []string) <-chan mirrorURLs {
// prepareMirrorURLsTypeC - C:
func prepareMirrorURLsTypeC(sourceURL string, targetURLs []string) <-chan mirrorURLs {
mirrorURLsCh := make(chan mirrorURLs)
go func() {
defer close(mirrorURLsCh)
@@ -349,24 +350,24 @@ func prepareCastURLsTypeC(sourceURL string, targetURLs []string) <-chan mirrorUR
newTargetURLParse.Path = filepath.Join(newTargetURLParse.Path, sourceContentName)
newTargetURLs = append(newTargetURLs, newTargetURLParse.String())
}
mirrorURLsCh <- prepareCastURLsTypeA(sourceContentParse.String(), newTargetURLs)
mirrorURLsCh <- prepareMirrorURLsTypeA(sourceContentParse.String(), newTargetURLs)
}
}()
return mirrorURLsCh
}
// prepareCastURLs - prepares target and source URLs for mirroring.
func prepareCastURLs(sourceURL string, targetURLs []string) <-chan mirrorURLs {
// prepareMirrorURLs - prepares target and source URLs for mirroring.
func prepareMirrorURLs(sourceURL string, targetURLs []string) <-chan mirrorURLs {
mirrorURLsCh := make(chan mirrorURLs)
go func() {
defer close(mirrorURLsCh)
switch guessCastURLType(sourceURL, targetURLs) {
switch guessMirrorURLType(sourceURL, targetURLs) {
case mirrorURLsTypeA:
mirrorURLsCh <- prepareCastURLsTypeA(sourceURL, targetURLs)
mirrorURLsCh <- prepareMirrorURLsTypeA(sourceURL, targetURLs)
case mirrorURLsTypeB:
mirrorURLsCh <- prepareCastURLsTypeB(sourceURL, targetURLs)
mirrorURLsCh <- prepareMirrorURLsTypeB(sourceURL, targetURLs)
case mirrorURLsTypeC:
for sURLs := range prepareCastURLsTypeC(sourceURL, targetURLs) {
for sURLs := range prepareMirrorURLsTypeC(sourceURL, targetURLs) {
mirrorURLsCh <- sURLs
}
default:
+3 -3
View File
@@ -106,8 +106,8 @@ func (c CopyMessage) String() string {
return console.JSON(string(copyMessageBytes) + "\n")
}
// CastMessage container for file mirror messages
type CastMessage struct {
// MirrorMessage container for file mirror messages
type MirrorMessage struct {
Version string `json:"version"`
Source string `json:"source"`
Targets []string `json:"targets"`
@@ -115,7 +115,7 @@ type CastMessage struct {
}
// String string printer for mirror message
func (s CastMessage) String() string {
func (s MirrorMessage) String() string {
if !globalJSONFlag {
return fmt.Sprintf("‘%s’ -> ‘%s’\n", s.Source, s.Targets)
}
+1 -1
View File
@@ -111,7 +111,7 @@ func sessionExecute(s *sessionV2) {
case "cp":
doCopyCmdSession(s)
case "mirror":
doCastCmdSession(s)
doMirrorCmdSession(s)
}
}