From ee65cf7ab818d130c43597ea2677284130ea9789 Mon Sep 17 00:00:00 2001 From: Harshavardhana Date: Tue, 29 Sep 2015 00:14:55 -0700 Subject: [PATCH] Fix bugs related to cp, mirror code argument validation --- common-methods.go | 9 +++++++++ cp-main.go | 21 ++++++--------------- cp-url.go | 4 ++++ diff.go | 3 +-- mirror-main.go | 20 ++++++-------------- pkg/console/console.go | 6 ++++++ 6 files changed, 32 insertions(+), 31 deletions(-) diff --git a/common-methods.go b/common-methods.go index 035137da..5c4b12aa 100644 --- a/common-methods.go +++ b/common-methods.go @@ -204,3 +204,12 @@ func target2Client(targetURL string) (client.Client, *probe.Error) { } return targetClient, nil } + +// bucketExists returns error +func bucketExists(URL string) *probe.Error { + _, _, err := url2Stat(URL) + if err != nil { + return err.Trace() + } + return nil +} diff --git a/cp-main.go b/cp-main.go index 7b834292..9e7b83fc 100644 --- a/cp-main.go +++ b/cp-main.go @@ -178,8 +178,7 @@ func doPrepareCopyURLs(session *sessionV2, trapCh <-chan bool) { if cpURLs.Error != nil { // Print in new line and adjust to top so that we don't print over the ongoing scan bar if !globalQuietFlag && !globalJSONFlag { - console.Printf("%c[2K\n", 27) - console.Printf("%c[A", 27) + console.Eraseline() } errorIf(cpURLs.Error.Trace(), "Unable to prepare URL for copying.") break @@ -187,7 +186,7 @@ func doPrepareCopyURLs(session *sessionV2, trapCh <-chan bool) { jsonData, err := json.Marshal(cpURLs) if err != nil { - session.Close() + session.Delete() fatalIf(probe.NewError(err), "Unable to prepare URL for copying. Error in JSON marshaling.") } fmt.Fprintln(dataFP, string(jsonData)) @@ -200,11 +199,9 @@ func doPrepareCopyURLs(session *sessionV2, trapCh <-chan bool) { case <-trapCh: // Print in new line and adjust to top so that we don't print over the ongoing scan bar if !globalQuietFlag && !globalJSONFlag { - console.Printf("%c[2K\n", 27) - console.Printf("%c[A", 27) + console.Eraseline() } - session.Close() // If we are interrupted during the URL scanning, we drop the session. - session.Delete() + session.Delete() // If we are interrupted during the URL scanning, we drop the session. os.Exit(0) } } @@ -259,8 +256,7 @@ func doCopyCmdSession(session *sessionV2) { } else { // Print in new line and adjust to top so that we don't print over the ongoing progress bar if !globalQuietFlag && !globalJSONFlag { - console.Printf("%c[2K\n", 27) - console.Printf("%c[A", 27) + console.Eraseline() } switch cpURLs.Error.ToGoError().(type) { // handle this specifically for filesystem @@ -282,10 +278,8 @@ func doCopyCmdSession(session *sessionV2) { os.Exit(0) } case <-trapCh: // Receive interrupt notification. - // Print in new line and adjust to top so that we don't print over the ongoing progress bar if !globalQuietFlag && !globalJSONFlag { - console.Printf("%c[2K\n", 27) - console.Printf("%c[A", 27) + console.Eraseline() } session.Close() session.Info() @@ -336,7 +330,6 @@ func mainCopy(ctx *cli.Context) { session.Header.CommandType = "cp" session.Header.RootPath, e = os.Getwd() if e != nil { - session.Close() session.Delete() fatalIf(probe.NewError(e), "Unable to get current working folder.") } @@ -345,12 +338,10 @@ func mainCopy(ctx *cli.Context) { var err *probe.Error session.Header.CommandArgs, err = args2URLs(ctx.Args()) if err != nil { - session.Close() session.Delete() fatalIf(err.Trace(), "One or more unknown URL types passed.") } doCopyCmdSession(session) - session.Close() session.Delete() } diff --git a/cp-url.go b/cp-url.go index 5ab5c377..9643d1e0 100644 --- a/cp-url.go +++ b/cp-url.go @@ -77,9 +77,13 @@ func checkCopySyntax(ctx *cli.Context) { { url := client.NewURL(tgtURL) if url.Host != "" { + // This check is for type URL. if url.Path == string(url.Separator) { fatalIf(errInvalidArgument().Trace(), fmt.Sprintf("Target ‘%s’ does not contain bucket name.", tgtURL)) } + if err := bucketExists(tgtURL); err != nil { + fatalIf(err.Trace(), fmt.Sprintf("Unable to stat target ‘%s’.", tgtURL)) + } } } switch guessCopyURLType(srcURLs, tgtURL) { diff --git a/diff.go b/diff.go index 78de815c..fd5c0e48 100644 --- a/diff.go +++ b/diff.go @@ -270,8 +270,7 @@ func dodiffRecursive(firstClnt, secondClnt client.Client, ch chan DiffMessage) { wg.Wait() doneCh <- struct{}{} if !globalQuietFlag && !globalJSONFlag { - console.Printf("%c[2K\n", 27) - console.Printf("%c[A", 27) + console.Eraseline() } matchNameCh := make(chan string, 10000) diff --git a/mirror-main.go b/mirror-main.go index 4d102fc3..7d17045f 100644 --- a/mirror-main.go +++ b/mirror-main.go @@ -174,8 +174,7 @@ func doPrepareMirrorURLs(session *sessionV2, trapCh <-chan bool) { if sURLs.Error != nil { // Print in new line and adjust to top so that we don't print over the ongoing scan bar if !globalQuietFlag && !globalJSONFlag { - console.Printf("%c[2K\n", 27) - console.Printf("%c[A", 27) + console.Eraseline() } errorIf(sURLs.Error.Trace(), "Unable to prepare URLs for mirroring.") break @@ -185,7 +184,7 @@ func doPrepareMirrorURLs(session *sessionV2, trapCh <-chan bool) { } jsonData, err := json.Marshal(sURLs) if err != nil { - session.Close() + session.Delete() fatalIf(probe.NewError(err), "Unable to marshal URLs into JSON.") } fmt.Fprintln(dataFP, string(jsonData)) @@ -198,11 +197,9 @@ func doPrepareMirrorURLs(session *sessionV2, trapCh <-chan bool) { case <-trapCh: // Print in new line and adjust to top so that we don't print over the ongoing scan bar if !globalQuietFlag && !globalJSONFlag { - console.Printf("%c[2K\n", 27) - console.Printf("%c[A", 27) + console.Eraseline() } - session.Close() // If we are interrupted during the URL scanning, we drop the session. - session.Delete() + session.Delete() // If we are interrupted during the URL scanning, we drop the session. os.Exit(0) } } @@ -257,8 +254,7 @@ func doMirrorCmdSession(session *sessionV2) { } else { // Print in new line and adjust to top so that we don't print over the ongoing progress bar if !globalQuietFlag && !globalJSONFlag { - console.Printf("%c[2K\n", 27) - console.Printf("%c[A", 27) + console.Eraseline() } switch sURLs.Error.ToGoError().(type) { // handle this specifically for filesystem @@ -282,8 +278,7 @@ func doMirrorCmdSession(session *sessionV2) { case <-trapCh: // Receive interrupt notification. // Print in new line and adjust to top so that we don't print over the ongoing progress bar if !globalQuietFlag && !globalJSONFlag { - console.Printf("%c[2K\n", 27) - console.Printf("%c[A", 27) + console.Eraseline() } session.Close() session.Info() @@ -333,7 +328,6 @@ func mainMirror(ctx *cli.Context) { session.Header.CommandType = "mirror" session.Header.RootPath, e = os.Getwd() if e != nil { - session.Close() session.Delete() fatalIf(probe.NewError(e), "Unable to get current working folder.") } @@ -342,12 +336,10 @@ func mainMirror(ctx *cli.Context) { var err *probe.Error session.Header.CommandArgs, err = args2URLs(ctx.Args()) if err != nil { - session.Close() session.Delete() fatalIf(err.Trace(ctx.Args()...), fmt.Sprintf("One or more unknown argument types found in ‘%s’.", ctx.Args())) } doMirrorCmdSession(session) - session.Close() session.Delete() } diff --git a/pkg/console/console.go b/pkg/console/console.go index 16d9dc00..3c7e4fa9 100644 --- a/pkg/console/console.go +++ b/pkg/console/console.go @@ -200,6 +200,12 @@ var ( } return fmt.Sprint(data) } + + // Eraseline Print in new line and adjust to top so that we don't print over the ongoing progress bar + Eraseline = func() { + consolePrintf("Print", Theme["Print"], "%c[2K\n", 27) + consolePrintf("Print", Theme["Print"], "%c[A", 27) + } ) // wrap around standard fmt functions