diff --git a/cmd-copy.go b/cmd-cp.go similarity index 81% rename from cmd-copy.go rename to cmd-cp.go index db44b9c9..d8c23bdf 100644 --- a/cmd-copy.go +++ b/cmd-cp.go @@ -63,6 +63,24 @@ func args2URLs(args cli.Args) ([]string, error) { return URLs, nil } +func runCopyInRoutine(cpurls *cpURLs, bar *barSend, cpQueue chan bool, wg *sync.WaitGroup) { + defer wg.Done() + srcConfig, err := getHostConfig(cpurls.SourceContent.Name) + if err != nil { + console.Errorln(iodine.ToError(err)) + return + } + tgtConfig, err := getHostConfig(cpurls.TargetContent.Name) + if err != nil { + console.Errorln(iodine.ToError(err)) + return + } + if err := doCopy(cpurls.SourceContent.Name, srcConfig, cpurls.TargetContent.Name, tgtConfig, bar); err != nil { + console.Errorln(iodine.ToError(err)) + } + <-cpQueue +} + // runCopyCmd is bound to sub-command func runCopyCmd(ctx *cli.Context) { if len(ctx.Args()) < 2 || ctx.Args().First() == "help" { @@ -85,7 +103,7 @@ func runCopyCmd(ctx *cli.Context) { var bar barSend // set up progress bar if !globalQuietFlag { - bar = newCopyBar() + bar = newCpBar() } go func(sourceURLs []string, targetURL string) { @@ -101,8 +119,8 @@ func runCopyCmd(ctx *cli.Context) { } }(sourceURLs, targetURL) - var cpQueue = make(chan bool, intMax(runtime.NumCPU()-1, 1)) - var wg sync.WaitGroup + cpQueue := make(chan bool, intMax(runtime.NumCPU()-1, 1)) + wg := new(sync.WaitGroup) for cpURLs := range prepareCopyURLs(sourceURLs, targetURL) { if cpURLs.Error != nil { @@ -111,24 +129,9 @@ func runCopyCmd(ctx *cli.Context) { } cpQueue <- true wg.Add(1) - go func(cpURLs *copyURLs, bar *barSend, cpQueue chan bool) { - defer wg.Done() - srcConfig, err := getHostConfig(cpURLs.SourceContent.Name) - if err != nil { - console.Errorln(iodine.ToError(err)) - return - } - tgtConfig, err := getHostConfig(cpURLs.TargetContent.Name) - if err != nil { - console.Errorln(iodine.ToError(err)) - return - } - if err := doCopy(cpURLs.SourceContent.Name, srcConfig, cpURLs.TargetContent.Name, tgtConfig, bar); err != nil { - console.Errorln(iodine.ToError(err)) - } - <-cpQueue - }(cpURLs, &bar, cpQueue) + go runCopyInRoutine(cpURLs, &bar, cpQueue, wg) } + close(cpQueue) wg.Wait() if !globalQuietFlag { bar.Finish() diff --git a/cmd-sync.go b/cmd-sync.go index 6fd78a49..006eea64 100644 --- a/cmd-sync.go +++ b/cmd-sync.go @@ -45,7 +45,7 @@ func runSyncCmd(ctx *cli.Context) { var bar barSend // set up progress bar if !globalQuietFlag { - bar = newCopyBar() + bar = newCpBar() } go func(sourceURL string, targetURLs []string) { @@ -71,7 +71,7 @@ func runSyncCmd(ctx *cli.Context) { } syncQueue <- true wg.Add(1) - go func(syncURLs *copyURLs, bar *barSend) { + go func(syncURLs *cpURLs, bar *barSend) { defer wg.Done() srcConfig, err := getHostConfig(syncURLs.SourceContent.Name) if err != nil { diff --git a/copy-bar.go b/cp-bar.go similarity index 82% rename from copy-bar.go rename to cp-bar.go index 1bd76162..0924b378 100644 --- a/copy-bar.go +++ b/cp-bar.go @@ -24,12 +24,12 @@ import ( "github.com/minio/mc/pkg/console" ) -type copyBarCmd int +type cpBarCmd int const ( - copyBarCmdExtend copyBarCmd = iota - copyBarCmdProgress - copyBarCmdFinish + cpBarCmdExtend cpBarCmd = iota + cpBarCmdProgress + cpBarCmdFinish ) type copyReader struct { @@ -44,7 +44,7 @@ func (r *copyReader) Read(p []byte) (n int, err error) { } type barMsg struct { - Cmd copyBarCmd + Cmd cpBarCmd Arg interface{} } @@ -54,11 +54,11 @@ type barSend struct { } func (b barSend) Extend(total int64) { - b.cmdCh <- barMsg{Cmd: copyBarCmdExtend, Arg: total} + b.cmdCh <- barMsg{Cmd: cpBarCmdExtend, Arg: total} } func (b barSend) Progress(progress int64) { - b.cmdCh <- barMsg{Cmd: copyBarCmdProgress, Arg: progress} + b.cmdCh <- barMsg{Cmd: cpBarCmdProgress, Arg: progress} } func (b *barSend) NewProxyReader(r io.Reader) *copyReader { @@ -67,12 +67,12 @@ func (b *barSend) NewProxyReader(r io.Reader) *copyReader { func (b barSend) Finish() { defer close(b.cmdCh) - b.cmdCh <- barMsg{Cmd: copyBarCmdFinish} + b.cmdCh <- barMsg{Cmd: cpBarCmdFinish} <-b.finishCh } -// newCopyBar - instantiate a copyBar. -func newCopyBar() barSend { +// newCpBar - instantiate a cpBar. +func newCpBar() barSend { cmdCh := make(chan barMsg) finishCh := make(chan bool) @@ -93,15 +93,15 @@ func newCopyBar() barSend { bar.Format("[=> ]") for msg := range cmdCh { switch msg.Cmd { - case copyBarCmdExtend: + case cpBarCmdExtend: bar.Total += msg.Arg.(int64) if bar.Total > 0 && !started { started = true bar.Start() } - case copyBarCmdProgress: + case cpBarCmdProgress: bar.Add64(msg.Arg.(int64)) - case copyBarCmdFinish: + case cpBarCmdFinish: if started { bar.Finish() } diff --git a/copy-url.go b/cp-url.go similarity index 68% rename from copy-url.go rename to cp-url.go index 4a7048f2..e6eb4005 100644 --- a/copy-url.go +++ b/cp-url.go @@ -49,20 +49,20 @@ import ( // ================================== // copy(*, f) -type copyURLs struct { +type cpURLs struct { SourceContent *client.Content TargetContent *client.Content Error error } -type copyURLsType uint8 +type cpURLsType uint8 const ( - copyURLsTypeInvalid copyURLsType = iota - copyURLsTypeA - copyURLsTypeB - copyURLsTypeC - copyURLsTypeD + cpURLsTypeInvalid cpURLsType = iota + cpURLsTypeA + cpURLsTypeB + cpURLsTypeC + cpURLsTypeD ) // source2Client returns client and hostconfig objects from the source URL. @@ -136,85 +136,85 @@ func isTargetURLDir(targetURL string) bool { // SINGLE SOURCE - Type A: copy(f, f) -> copy(f, f) // guessCopyURLType guesses the type of URL. This approach all allows prepareURL // functions to accurately report failure causes. -func guessCopyURLType(sourceURLs []string, targetURL string) copyURLsType { +func guessCopyURLType(sourceURLs []string, targetURL string) cpURLsType { if targetURL == "" { // Target is empty - return copyURLsTypeInvalid + return cpURLsTypeInvalid } if len(sourceURLs) < 1 { // Source list is empty - return copyURLsTypeInvalid + return cpURLsTypeInvalid } if len(sourceURLs) == 1 { // 1 Source, 1 Target if isURLRecursive(sourceURLs[0]) { // Type C - return copyURLsTypeC + return cpURLsTypeC } // else Type A or Type B if isTargetURLDir(targetURL) { // Type B - return copyURLsTypeB + return cpURLsTypeB } // else Type A - return copyURLsTypeA + return cpURLsTypeA } // else Type D - return copyURLsTypeD + return cpURLsTypeD } // prepareCopyURLsTypeA - prepares target and source URLs for copying. -func prepareCopyURLsTypeA(sourceURL string, targetURL string) *copyURLs { +func prepareCopyURLsTypeA(sourceURL string, targetURL string) *cpURLs { sourceClient, err := source2Client(sourceURL) if err != nil { - return ©URLs{Error: iodine.New(err, nil)} + return &cpURLs{Error: iodine.New(err, nil)} } // Source exist? sourceContent, err := sourceClient.Stat() if err != nil { // Source does not exist or insufficient privileges. - return ©URLs{Error: iodine.New(err, nil)} + return &cpURLs{Error: iodine.New(err, nil)} } if !sourceContent.Type.IsRegular() { // Source is not a regular file - return ©URLs{Error: iodine.New(errInvalidSource{path: sourceURL}, nil)} + return &cpURLs{Error: iodine.New(errInvalidSource{path: sourceURL}, nil)} } targetClient, err := target2Client(targetURL) if err != nil { - return ©URLs{Error: iodine.New(err, nil)} + return &cpURLs{Error: iodine.New(err, nil)} } // Target exist? targetContent, err := targetClient.Stat() if err == nil { // Target exists. if !targetContent.Type.IsRegular() { // Target is not a regular file - return ©URLs{Error: iodine.New(errInvalidTarget{path: targetURL}, nil)} + return &cpURLs{Error: iodine.New(errInvalidTarget{path: targetURL}, nil)} } // Target exists but, we can may overwrite. Let the copy function decide. } // Target does not exist. We can create a new target file | object here. // All OK.. We can proceed. Type A - return ©URLs{SourceContent: sourceContent, TargetContent: &client.Content{Name: targetURL}} + return &cpURLs{SourceContent: sourceContent, TargetContent: &client.Content{Name: targetURL}} } // SINGLE SOURCE - Type B: copy(f, d) -> copy(f, d/f) -> A // prepareCopyURLsTypeB - prepares target and source URLs for copying. -func prepareCopyURLsTypeB(sourceURL string, targetURL string) *copyURLs { +func prepareCopyURLsTypeB(sourceURL string, targetURL string) *cpURLs { sourceClient, err := source2Client(sourceURL) if err != nil { - return ©URLs{Error: iodine.New(err, nil)} + return &cpURLs{Error: iodine.New(err, nil)} } sourceContent, err := sourceClient.Stat() if err != nil { // Source does not exist or insufficient privileges. - return ©URLs{Error: iodine.New(err, nil)} + return &cpURLs{Error: iodine.New(err, nil)} } if !sourceContent.Type.IsRegular() { // Source is not a regular file. - return ©URLs{Error: iodine.New(errInvalidSource{path: sourceURL}, nil)} + return &cpURLs{Error: iodine.New(errInvalidSource{path: sourceURL}, nil)} } targetClient, err := target2Client(targetURL) if err != nil { - return ©URLs{Error: iodine.New(err, nil)} + return &cpURLs{Error: iodine.New(err, nil)} } // Target exist? @@ -222,19 +222,19 @@ func prepareCopyURLsTypeB(sourceURL string, targetURL string) *copyURLs { if err == nil { if !targetContent.Type.IsDir() { // Target exists, but is not a directory. - return ©URLs{Error: iodine.New(fmt.Errorf("Target [%s] is not a directory.", targetURL), nil)} + return &cpURLs{Error: iodine.New(fmt.Errorf("Target [%s] is not a directory.", targetURL), nil)} } } // Else name is available to create. // All OK.. We can proceed. Type B: source is a file, target is a directory and exists. sourceURLParse := client.Parse(sourceURL) if sourceURLParse == nil { - return ©URLs{Error: iodine.New(errInvalidSource{path: sourceURL}, nil)} + return &cpURLs{Error: iodine.New(errInvalidSource{path: sourceURL}, nil)} } targetURLParse := client.Parse(targetURL) if targetURLParse == nil { - return ©URLs{Error: iodine.New(errInvalidTarget{path: targetURL}, nil)} + return &cpURLs{Error: iodine.New(errInvalidTarget{path: targetURL}, nil)} } targetURLParse.Path = filepath.Join(targetURLParse.Path, filepath.Base(sourceURLParse.Path)) @@ -243,20 +243,20 @@ func prepareCopyURLsTypeB(sourceURL string, targetURL string) *copyURLs { // SINGLE SOURCE - Type C: copy(d1..., d2) -> []copy(d1/f, d1/d2/f) -> []A // prepareCopyRecursiveURLTypeC - prepares target and source URLs for copying. -func prepareCopyURLsTypeC(sourceURL, targetURL string) <-chan *copyURLs { - copyURLsCh := make(chan *copyURLs) - go func(sourceURL, targetURL string, copyURLsCh chan *copyURLs) { - defer close(copyURLsCh) +func prepareCopyURLsTypeC(sourceURL, targetURL string) <-chan *cpURLs { + cpURLsCh := make(chan *cpURLs) + go func(sourceURL, targetURL string, cpURLsCh chan *cpURLs) { + defer close(cpURLsCh) if !isURLRecursive(sourceURL) { // Source is not of recursive type. - copyURLsCh <- ©URLs{Error: iodine.New(fmt.Errorf("Source [%s] is not recursive.", sourceURL), nil)} + cpURLsCh <- &cpURLs{Error: iodine.New(fmt.Errorf("Source [%s] is not recursive.", sourceURL), nil)} return } sourceURL = stripRecursiveURL(sourceURL) sourceClient, err := source2Client(sourceURL) if err != nil { - copyURLsCh <- ©URLs{Error: iodine.New(err, nil)} + cpURLsCh <- &cpURLs{Error: iodine.New(err, nil)} return } @@ -264,19 +264,19 @@ func prepareCopyURLsTypeC(sourceURL, targetURL string) <-chan *copyURLs { sourceContent, err := sourceClient.Stat() if err != nil { // Source does not exist or insufficient privileges. - copyURLsCh <- ©URLs{Error: iodine.New(err, nil)} + cpURLsCh <- &cpURLs{Error: iodine.New(err, nil)} return } if !sourceContent.Type.IsDir() { // Source is not a dir. - copyURLsCh <- ©URLs{Error: iodine.New(fmt.Errorf("Source [%s] is not a directory.", sourceURL), nil)} + cpURLsCh <- &cpURLs{Error: iodine.New(fmt.Errorf("Source [%s] is not a directory.", sourceURL), nil)} return } targetClient, err := target2Client(targetURL) if err != nil { - copyURLsCh <- ©URLs{Error: iodine.New(err, nil)} + cpURLsCh <- &cpURLs{Error: iodine.New(err, nil)} return } @@ -284,20 +284,20 @@ func prepareCopyURLsTypeC(sourceURL, targetURL string) <-chan *copyURLs { targetContent, err := targetClient.Stat() if err != nil { // Target does not exist. - copyURLsCh <- ©URLs{Error: iodine.New(fmt.Errorf("Target directory [%s] does not exist.", targetURL), nil)} + cpURLsCh <- &cpURLs{Error: iodine.New(fmt.Errorf("Target directory [%s] does not exist.", targetURL), nil)} return } if !targetContent.Type.IsDir() { // Target exists, but is not a directory. - copyURLsCh <- ©URLs{Error: iodine.New(fmt.Errorf("Target [%s] is not a directory.", targetURL), nil)} + cpURLsCh <- &cpURLs{Error: iodine.New(fmt.Errorf("Target [%s] is not a directory.", targetURL), nil)} return } for sourceContent := range sourceClient.ListRecursive() { if sourceContent.Err != nil { // Listing failed. - copyURLsCh <- ©URLs{Error: iodine.New(sourceContent.Err, nil)} + cpURLsCh <- &cpURLs{Error: iodine.New(sourceContent.Err, nil)} continue } @@ -309,19 +309,19 @@ func prepareCopyURLsTypeC(sourceURL, targetURL string) <-chan *copyURLs { // All OK.. We can proceed. Type B: source is a file, target is a directory and exists. sourceURLParse := client.Parse(sourceURL) if sourceURLParse == nil { - copyURLsCh <- ©URLs{Error: iodine.New(errInvalidSource{path: sourceURL}, nil)} + cpURLsCh <- &cpURLs{Error: iodine.New(errInvalidSource{path: sourceURL}, nil)} continue } targetURLParse := client.Parse(targetURL) if targetURLParse == nil { - copyURLsCh <- ©URLs{Error: iodine.New(errInvalidTarget{path: targetURL}, nil)} + cpURLsCh <- &cpURLs{Error: iodine.New(errInvalidTarget{path: targetURL}, nil)} continue } sourceContentParse := client.Parse(sourceContent.Content.Name) if sourceContentParse == nil { - copyURLsCh <- ©URLs{Error: iodine.New(errInvalidSource{path: sourceContent.Content.Name}, nil)} + cpURLsCh <- &cpURLs{Error: iodine.New(errInvalidSource{path: sourceContent.Content.Name}, nil)} continue } @@ -329,24 +329,24 @@ func prepareCopyURLsTypeC(sourceURL, targetURL string) <-chan *copyURLs { newTargetURLParse := *targetURLParse newTargetURLParse.Path = filepath.Join(newTargetURLParse.Path, strings.TrimPrefix(sourceContentParse.Path, filepath.Dir(sourceURLParse.Path))) - copyURLsCh <- prepareCopyURLsTypeA(sourceContentParse.String(), newTargetURLParse.String()) + cpURLsCh <- prepareCopyURLsTypeA(sourceContentParse.String(), newTargetURLParse.String()) } - }(sourceURL, targetURL, copyURLsCh) + }(sourceURL, targetURL, cpURLsCh) - return copyURLsCh + return cpURLsCh } // MULTI-SOURCE - Type D: copy([]f, d) -> []B // prepareCopyURLsTypeD - prepares target and source URLs for copying. -func prepareCopyURLsTypeD(sourceURLs []string, targetURL string) <-chan *copyURLs { - copyURLsCh := make(chan *copyURLs) +func prepareCopyURLsTypeD(sourceURLs []string, targetURL string) <-chan *cpURLs { + cpURLsCh := make(chan *cpURLs) - go func(sourceURLs []string, targetURL string, copyURLsCh chan *copyURLs) { - defer close(copyURLsCh) + go func(sourceURLs []string, targetURL string, cpURLsCh chan *cpURLs) { + defer close(cpURLsCh) targetClient, err := target2Client(targetURL) if err != nil { - copyURLsCh <- ©URLs{Error: iodine.New(err, nil)} + cpURLsCh <- &cpURLs{Error: iodine.New(err, nil)} return } @@ -354,18 +354,18 @@ func prepareCopyURLsTypeD(sourceURLs []string, targetURL string) <-chan *copyURL targetContent, err := targetClient.Stat() if err != nil { // Target does not exist. - copyURLsCh <- ©URLs{Error: iodine.New(fmt.Errorf("Target directory [%s] does not exist.", targetURL), nil)} + cpURLsCh <- &cpURLs{Error: iodine.New(fmt.Errorf("Target directory [%s] does not exist.", targetURL), nil)} return } if !targetContent.Type.IsDir() { // Target exists, but is not a directory. - copyURLsCh <- ©URLs{Error: iodine.New(fmt.Errorf("Target [%s] is not a directory.", targetURL), nil)} + cpURLsCh <- &cpURLs{Error: iodine.New(fmt.Errorf("Target [%s] is not a directory.", targetURL), nil)} return } if len(sourceURLs) < 1 { // Source list is empty. - copyURLsCh <- ©URLs{Error: iodine.New(errors.New("Source list is empty"), nil)} + cpURLsCh <- &cpURLs{Error: iodine.New(errors.New("Source list is empty"), nil)} return } @@ -374,44 +374,44 @@ func prepareCopyURLsTypeD(sourceURLs []string, targetURL string) <-chan *copyURL // Is it a recursive URL "..."? if isURLRecursive(sourceURL) { // Type C ch := prepareCopyURLsTypeC(sourceURL, targetURL) - for copyURLs := range ch { - copyURLsCh <- copyURLs + for cpURLs := range ch { + cpURLsCh <- cpURLs } } else { // Type B - copyURLsCh <- prepareCopyURLsTypeB(sourceURL, targetURL) + cpURLsCh <- prepareCopyURLsTypeB(sourceURL, targetURL) } } - }(sourceURLs, targetURL, copyURLsCh) + }(sourceURLs, targetURL, cpURLsCh) - return copyURLsCh + return cpURLsCh } // prepareCopyURLs - prepares target and source URLs for copying. -func prepareCopyURLs(sourceURLs []string, targetURL string) <-chan *copyURLs { - copyURLsCh := make(chan *copyURLs) +func prepareCopyURLs(sourceURLs []string, targetURL string) <-chan *cpURLs { + cpURLsCh := make(chan *cpURLs) - go func(sourceURLs []string, targetURL string, copyURLsCh chan *copyURLs) { - defer close(copyURLsCh) + go func(sourceURLs []string, targetURL string, cpURLsCh chan *cpURLs) { + defer close(cpURLsCh) switch guessCopyURLType(sourceURLs, targetURL) { - case copyURLsTypeA: - copyURLs := prepareCopyURLsTypeA(sourceURLs[0], targetURL) - copyURLsCh <- copyURLs - case copyURLsTypeB: - copyURLs := prepareCopyURLsTypeB(sourceURLs[0], targetURL) - copyURLsCh <- copyURLs - case copyURLsTypeC: - for copyURLs := range prepareCopyURLsTypeC(sourceURLs[0], targetURL) { - copyURLsCh <- copyURLs + case cpURLsTypeA: + cpURLs := prepareCopyURLsTypeA(sourceURLs[0], targetURL) + cpURLsCh <- cpURLs + case cpURLsTypeB: + cpURLs := prepareCopyURLsTypeB(sourceURLs[0], targetURL) + cpURLsCh <- cpURLs + case cpURLsTypeC: + for cpURLs := range prepareCopyURLsTypeC(sourceURLs[0], targetURL) { + cpURLsCh <- cpURLs } - case copyURLsTypeD: - for copyURLs := range prepareCopyURLsTypeD(sourceURLs, targetURL) { - copyURLsCh <- copyURLs + case cpURLsTypeD: + for cpURLs := range prepareCopyURLsTypeD(sourceURLs, targetURL) { + cpURLsCh <- cpURLs } default: - copyURLsCh <- ©URLs{Error: iodine.New(errors.New("Invalid arguments."), nil)} + cpURLsCh <- &cpURLs{Error: iodine.New(errors.New("Invalid arguments."), nil)} } - }(sourceURLs, targetURL, copyURLsCh) + }(sourceURLs, targetURL, cpURLsCh) - return copyURLsCh + return cpURLsCh } diff --git a/sync-url.go b/sync-url.go index d91a0cff..ea2bdc69 100644 --- a/sync-url.go +++ b/sync-url.go @@ -44,28 +44,28 @@ import ( // sync(*, f1) // sync(*, []f1) -type syncURLs copyURLs +type syncURLs cpURLs // prepareCopyURLs - prepares target and source URLs for syncing. -func prepareSyncURLs(sourceURL string, targetURLs []string) <-chan *copyURLs { - syncURLsCh := make(chan *copyURLs) +func prepareSyncURLs(sourceURL string, targetURLs []string) <-chan *cpURLs { + syncURLsCh := make(chan *cpURLs) go func() { defer close(syncURLsCh) for _, targetURL := range targetURLs { switch guessCopyURLType([]string{sourceURL}, targetURL) { - case copyURLsTypeA: + case cpURLsTypeA: syncURLs := prepareCopyURLsTypeA(sourceURL, targetURL) syncURLsCh <- syncURLs - case copyURLsTypeB: + case cpURLsTypeB: syncURLs := prepareCopyURLsTypeB(sourceURL, targetURL) syncURLsCh <- syncURLs - case copyURLsTypeC: + case cpURLsTypeC: for syncURLs := range prepareCopyURLsTypeC(sourceURL, targetURL) { syncURLsCh <- syncURLs } default: - syncURLsCh <- ©URLs{Error: iodine.New(errors.New("Invalid arguments."), nil)} + syncURLsCh <- &cpURLs{Error: iodine.New(errors.New("Invalid arguments."), nil)} } } }()