From 9e94120d68906e292e79831ad35f1163746bb1e8 Mon Sep 17 00:00:00 2001 From: Harshavardhana Date: Sun, 17 Apr 2016 02:51:48 -0700 Subject: [PATCH] mirror: Implement '--remove' feature. Fixes #1628 --- .travis.yml | 3 +- mirror-main.go | 82 +++++++++++++++++++++++++++++++++++++++++-------- mirror-url.go | 29 ++++++++++++----- typed-errors.go | 3 ++ 4 files changed, 96 insertions(+), 21 deletions(-) diff --git a/.travis.yml b/.travis.yml index 4eeb1d34..f1b6985f 100644 --- a/.travis.yml +++ b/.travis.yml @@ -16,7 +16,8 @@ go: - 1.6 script: -- make +- make test +- make test GOFLAGS="-race" - diff -au <(gofmt -d *.go) <(printf "") - diff -au <(gofmt -d pkg) <(printf "") diff --git a/mirror-main.go b/mirror-main.go index 721d1a93..c9a1e258 100644 --- a/mirror-main.go +++ b/mirror-main.go @@ -48,6 +48,10 @@ var ( Name: "fake", Usage: "Perform a fake mirror operation.", }, + cli.BoolFlag{ + Name: "remove", + Usage: "Remove extraneous file(s) on target.", + }, } ) @@ -82,6 +86,9 @@ EXAMPLES: 5. Fake mirror a bucket from Minio cloud storage to a bucket on Amazon S3 cloud storage. $ mc {{.Name}} --fake play/photos/2014 s3/backup-photos/2014 + 6. Mirror a bucket from Minio cloud storage to a bucket on Amazon S3 cloud storage and remove any extraneous + files on Amazon S3 cloud storage. NOTE: '--remove' is only supported with '--force'. + $ mc {{.Name}} --force --remove play/photos/2014 s3/backup-photos/2014 `, } @@ -126,6 +133,25 @@ func (c mirrorStatMessage) String() string { return message } +// doRemove - removes files on target. +func doRemove(sURLs mirrorURLs, fakeRemove bool) mirrorURLs { + targetAlias := sURLs.TargetAlias + targetURL := sURLs.TargetContent.URL + + // We are not removing incomplete uploads. + isIncomplete := false + + // Remove extraneous file on target. + err := rm(targetAlias, targetURL.String(), isIncomplete, fakeRemove) + if err != nil { + sURLs.Error = err.Trace(targetAlias, targetURL.String()) + return sURLs + } + + sURLs.Error = nil // just for safety + return sURLs +} + // doMirror - Mirror an object to multiple destination. mirrorURLs status contains a copy of sURLs and error if any. func doMirror(sURLs mirrorURLs, progressReader *progressBar, accountingReader *accounter, fakeMirror bool) mirrorURLs { if sURLs.Error != nil { // Errorneous sURLs passed. @@ -188,7 +214,7 @@ func doMirror(sURLs mirrorURLs, progressReader *progressBar, accountingReader *a } // doPrepareMirrorURLs scans the source URL and prepares a list of objects for mirroring. -func doPrepareMirrorURLs(session *sessionV6, isForce bool, isFake bool, trapCh <-chan bool) { +func doPrepareMirrorURLs(session *sessionV6, isForce bool, isFake bool, isRemove bool, trapCh <-chan bool) { sourceURL := session.Header.CommandArgs[0] // first one is source. targetURL := session.Header.CommandArgs[1] var totalBytes int64 @@ -202,7 +228,7 @@ func doPrepareMirrorURLs(session *sessionV6, isForce bool, isFake bool, trapCh < scanBar = scanBarFactory() } - URLsCh := prepareMirrorURLs(sourceURL, targetURL, isForce, isFake) + URLsCh := prepareMirrorURLs(sourceURL, targetURL, isForce, isFake, isRemove) done := false for !done { select { @@ -229,10 +255,18 @@ func doPrepareMirrorURLs(session *sessionV6, isForce bool, isFake bool, trapCh < } fmt.Fprintln(dataFP, string(jsonData)) if !globalQuiet && !globalJSON { - scanBar(sURLs.SourceContent.URL.String()) + // Source content is empty if removal is requested, + // put targetContent on to scan bar. + if sURLs.SourceContent != nil { + scanBar(sURLs.SourceContent.URL.String()) + } else if sURLs.TargetContent != nil && isRemove { + scanBar(sURLs.TargetContent.URL.String()) + } + } + // Remember totalBytes only for mirror not for removal, + if sURLs.SourceContent != nil { + totalBytes += sURLs.SourceContent.Size } - - totalBytes += sURLs.SourceContent.Size totalObjects++ case <-trapCh: // Print in new line and adjust to top so that we don't print over the ongoing scan bar @@ -252,12 +286,13 @@ func doPrepareMirrorURLs(session *sessionV6, isForce bool, isFake bool, trapCh < func doMirrorSession(session *sessionV6) { isForce := session.Header.CommandBoolFlags["force"] isFake := session.Header.CommandBoolFlags["fake"] + isRemove := session.Header.CommandBoolFlags["remove"] // Initialize signal trap. trapCh := signalTrap(os.Interrupt, syscall.SIGTERM) if !session.HasData() { - doPrepareMirrorURLs(session, isForce, isFake, trapCh) + doPrepareMirrorURLs(session, isForce, isFake, isRemove, trapCh) } // Enable accounting reader by default. @@ -300,8 +335,20 @@ func doMirrorSession(session *sessionV6) { return } if sURLs.Error == nil { - session.Header.LastCopied = sURLs.SourceContent.URL.String() - session.Save() + if sURLs.SourceContent != nil { + session.Header.LastCopied = sURLs.SourceContent.URL.String() + session.Save() + } else if sURLs.TargetContent != nil && isRemove { + // Construct user facing message and path. + targetPath := filepath.ToSlash(filepath.Join(sURLs.TargetAlias, sURLs.TargetContent.URL.Path)) + if !globalQuiet && !globalJSON { + console.Eraseline() + } + printMsg(rmMessage{ + Status: "success", + URL: targetPath, + }) + } } else { // Print in new line and adjust to top so that we // don't print over the ongoing progress bar. @@ -331,13 +378,20 @@ func doMirrorSession(session *sessionV6) { // Loop through all urls and mirror. for urlScanner.Scan() { var sURLs mirrorURLs + // Unmarshal copyURLs from each line. json.Unmarshal([]byte(urlScanner.Text()), &sURLs) - // Verify if previously copied or if its a fake mirror, set - // fake mirror accordingly. - fakeMirror := isCopied(sURLs.SourceContent.URL.String()) || isFake - // Perform mirror operation. - statusCh <- doMirror(sURLs, progressReader, accntReader, fakeMirror) + + if sURLs.SourceContent != nil { + // Verify if previously copied or if its a fake mirror, set + // fake mirror accordingly. + fakeMirror := isCopied(sURLs.SourceContent.URL.String()) || isFake + // Perform mirror operation. + statusCh <- doMirror(sURLs, progressReader, accntReader, fakeMirror) + } else if sURLs.TargetContent != nil && isRemove { + // Perform remove operation. + statusCh <- doRemove(sURLs, isFake) + } } // Close the goroutine. @@ -384,8 +438,10 @@ func mainMirror(ctx *cli.Context) { // Set command flags from context. isForce := ctx.Bool("force") isFake := ctx.Bool("fake") + isRemove := ctx.Bool("remove") session.Header.CommandBoolFlags["force"] = isForce session.Header.CommandBoolFlags["fake"] = isFake + session.Header.CommandBoolFlags["remove"] = isRemove // extract URLs. session.Header.CommandArgs = ctx.Args() diff --git a/mirror-url.go b/mirror-url.go index 14db84ac..bc39ad99 100644 --- a/mirror-url.go +++ b/mirror-url.go @@ -36,8 +36,11 @@ func (m mirrorURLs) isEmpty() bool { if m.SourceContent == nil && m.TargetContent == nil && m.Error == nil { return true } - if m.SourceContent.Size == 0 && m.TargetContent == nil && m.Error == nil { - return true + // If remove flag is set then sourceContent is usually nil. + if m.SourceContent != nil { + if m.SourceContent.Size == 0 && m.TargetContent == nil && m.Error == nil { + return true + } } return false } @@ -90,7 +93,7 @@ func checkMirrorSyntax(ctx *cli.Context) { } } -func deltaSourceTarget(sourceURL string, targetURL string, isForce bool, isFake bool, mirrorURLsCh chan<- mirrorURLs) { +func deltaSourceTarget(sourceURL string, targetURL string, isForce bool, isFake bool, isRemove bool, mirrorURLsCh chan<- mirrorURLs) { // source and targets are always directories sourceSeparator := string(newClientURL(sourceURL).Separator) if !strings.HasSuffix(sourceURL, sourceSeparator) { @@ -157,13 +160,25 @@ func deltaSourceTarget(sourceURL string, targetURL string, isForce bool, isFake TargetAlias: targetAlias, TargetContent: targetContent, } - } // else if diffMsg.Diff == differInSecond { - // Add for bi-directional mirror. + } else if diffMsg.Diff == differInSecond && isRemove { + if !isForce && !isFake { + // Object removal not allowed if force is not set. + mirrorURLsCh <- mirrorURLs{ + Error: errDeleteNotAllowed(diffMsg.SecondURL), + } + continue + } + mirrorURLsCh <- mirrorURLs{ + TargetAlias: targetAlias, + TargetContent: diffMsg.secondContent, + } + } } } -func prepareMirrorURLs(sourceURL string, targetURL string, isForce bool, isFake bool) <-chan mirrorURLs { +// Prepares urls that need to be copied or removed based on requested options. +func prepareMirrorURLs(sourceURL string, targetURL string, isForce bool, isFake bool, isRemove bool) <-chan mirrorURLs { mirrorURLsCh := make(chan mirrorURLs) - go deltaSourceTarget(sourceURL, targetURL, isForce, isFake, mirrorURLsCh) + go deltaSourceTarget(sourceURL, targetURL, isForce, isFake, isRemove, mirrorURLsCh) return mirrorURLsCh } diff --git a/typed-errors.go b/typed-errors.go index f51ceaeb..40d6e0f8 100644 --- a/typed-errors.go +++ b/typed-errors.go @@ -51,6 +51,9 @@ var ( return probe.NewError(errors.New("Overwrite not allowed for ‘" + URL + "’. Use ‘--force’ to override this behavior.")) } + errDeleteNotAllowed = func(URL string) *probe.Error { + return probe.NewError(errors.New("Delete not allowed for ‘" + URL + "’. Use ‘--force’ to override this behavior.")) + } errSourceIsDir = func(URL string) *probe.Error { return probe.NewError(errors.New("Source ‘" + URL + "’ is a folder.")).Untrace() }