From f8eaa96b1d310d2ef7eba30a6434aa8e8e352fba Mon Sep 17 00:00:00 2001 From: Harshavardhana Date: Thu, 16 Jul 2015 22:38:21 -0700 Subject: [PATCH] Add force flag support to avoid over-writing destination --- cmd-cp.go | 8 +++++++- cmd-options.go | 5 +++++ cmd-sync.go | 10 +++++++++- cp-url.go | 2 ++ globals.go | 1 + main.go | 2 ++ sync-url.go | 3 +++ 7 files changed, 29 insertions(+), 2 deletions(-) diff --git a/cmd-cp.go b/cmd-cp.go index fdac8438..a7a3a002 100644 --- a/cmd-cp.go +++ b/cmd-cp.go @@ -147,6 +147,13 @@ func doPrepareCopyURLs(session *sessionV2, trapCh <-chan bool) { console.Errorln(cpURLs.Error) break } + if !globalForceFlag { + if cpURLs.TargetContent.Size != 0 || !cpURLs.TargetContent.Time.IsZero() { + console.Fatalf("Destination already exists, cannot overwrite ‘%s’ with ‘%s’. "+ + "Use ‘--force’ flag to override.\n", cpURLs.TargetContent.Name, cpURLs.SourceContent.Name) + break + } + } jsonData, err := json.Marshal(cpURLs) if err != nil { @@ -177,7 +184,6 @@ func doCopyCmdSession(session *sessionV2) { wg := new(sync.WaitGroup) cpQueue := make(chan bool, int(math.Max(float64(runtime.NumCPU())-1, 1))) - defer close(cpQueue) scanner := bufio.NewScanner(session.NewDataReader()) isCopied := isCopiedFactory(session.Header.LastCopied) diff --git a/cmd-options.go b/cmd-options.go index b424b16c..ee65f220 100644 --- a/cmd-options.go +++ b/cmd-options.go @@ -42,6 +42,11 @@ var ( Usage: "Suppress chatty console output", } + forceFlag = cli.BoolFlag{ + Name: "force", + Usage: "Force copying when destination exists", + } + aliasFlag = cli.BoolFlag{ Name: "alias", Usage: "Mimic operating system toolchain behavior wherever it makes sense", diff --git a/cmd-sync.go b/cmd-sync.go index b443f313..537410b6 100644 --- a/cmd-sync.go +++ b/cmd-sync.go @@ -147,6 +147,15 @@ func doPrepareSyncURLs(session *sessionV2, trapCh <-chan bool) { console.Errorln(sURLs.Error) break } + if !globalForceFlag { + if len(sURLs.TargetContents) > 0 { + if sURLs.TargetContents[0].Size != 0 || !sURLs.TargetContents[0].Time.IsZero() { + console.Fatalf("Destination already exists, cannot overwrite ‘%s’ with ‘%s’. "+ + "Use ‘--force’ flag to override.\n", sURLs.TargetContents[0].Name, sURLs.SourceContent.Name) + break + } + } + } jsonData, err := json.Marshal(sURLs) if err != nil { @@ -177,7 +186,6 @@ func doSyncCmdSession(session *sessionV2) { wg := new(sync.WaitGroup) syncQueue := make(chan bool, int(math.Max(float64(runtime.NumCPU())-1, 1))) - defer close(syncQueue) scanner := bufio.NewScanner(session.NewDataReader()) isCopied := isCopiedFactory(session.Header.LastCopied) diff --git a/cp-url.go b/cp-url.go index dec8f02a..c896c949 100644 --- a/cp-url.go +++ b/cp-url.go @@ -136,6 +136,8 @@ func prepareCopyURLsTypeA(sourceURL string, targetURL string) <-chan copyURLs { copyURLsCh <- copyURLs{Error: NewIodine(iodine.New(errInvalidTarget{URL: targetURL}, nil))} return } + copyURLsCh <- copyURLs{SourceContent: sourceContent, TargetContent: targetContent} + return } // All OK.. We can proceed. Type A sourceContent.Name = sourceURL diff --git a/globals.go b/globals.go index 870b728d..abe18898 100644 --- a/globals.go +++ b/globals.go @@ -21,6 +21,7 @@ import "github.com/minio/minio/pkg/iodine" var ( globalQuietFlag = false // Quiet flag set via command line + globalForceFlag = false // Force flag set via command line globalAliasFlag = false // Alias flag set via command line globalJSONFlag = false // Json flag set via command line globalDebugFlag = false // Debug flag set via command line diff --git a/main.go b/main.go index d9bbab79..c45015d5 100644 --- a/main.go +++ b/main.go @@ -105,6 +105,7 @@ func main() { // register all the flags registerFlag(configFlag) // path to config folder registerFlag(quietFlag) // suppress console output + registerFlag(forceFlag) // force copying data registerFlag(aliasFlag) // OS toolchain mimic registerFlag(themeFlag) // console theme flag registerFlag(jsonFlag) // json formatted output @@ -123,6 +124,7 @@ func main() { } globalQuietFlag = ctx.GlobalBool("quiet") + globalForceFlag = ctx.GlobalBool("force") globalAliasFlag = ctx.GlobalBool("alias") globalDebugFlag = ctx.GlobalBool("debug") globalJSONFlag = ctx.GlobalBool("json") diff --git a/sync-url.go b/sync-url.go index 656bcd88..98313b7c 100644 --- a/sync-url.go +++ b/sync-url.go @@ -100,6 +100,9 @@ func prepareSingleSyncURLsTypeA(sourceURL string, targetURL string) syncURLs { if !targetContent.Type.IsRegular() { // Target is not a regular file return syncURLs{Error: NewIodine(iodine.New(errInvalidTarget{URL: targetURL}, nil))} } + var targetContents []*client.Content + targetContents = append(targetContents, targetContent) + return syncURLs{SourceContent: sourceContent, TargetContents: targetContents} } // All OK.. We can proceed. Type A sourceContent.Name = sourceURL