From 685299ab754280c08ea52b95b5dc7a5ceab12545 Mon Sep 17 00:00:00 2001 From: Harshavardhana Date: Wed, 21 Jun 2017 16:34:19 -0700 Subject: [PATCH] mirror: Add timestamp check similar to aws s3 sync (#2188) Currently `mc mirror` requires source to be uploaded only if the size is different than the object on remote server. This PR adds support to check for last modified time of the source object as well such that we upload if there are timestamp differences as well. Fixes #2187 --- cmd/diff-main.go | 3 +++ cmd/difference.go | 19 +++++++++++++++++-- cmd/mirror-url.go | 2 +- 3 files changed, 21 insertions(+), 3 deletions(-) diff --git a/cmd/diff-main.go b/cmd/diff-main.go index 64953d94..d3610db7 100644 --- a/cmd/diff-main.go +++ b/cmd/diff-main.go @@ -91,6 +91,8 @@ func (d diffMessage) String() string { msg = console.Colorize("DiffType", "! "+d.SecondURL) case differInSize: msg = console.Colorize("DiffSize", "! "+d.SecondURL) + case differInTime: + msg = console.Colorize("DiffTime", "! "+d.SecondURL) default: fatalIf(errDummy().Trace(d.FirstURL, d.SecondURL), "Unhandled difference between `"+d.FirstURL+"` and `"+d.SecondURL+"`.") @@ -198,6 +200,7 @@ func mainDiff(ctx *cli.Context) error { console.SetColor("DiffOnlyInSecond", color.New(color.FgGreen)) console.SetColor("DiffType", color.New(color.FgMagenta)) console.SetColor("DiffSize", color.New(color.FgYellow, color.Bold)) + console.SetColor("DiffTime", color.New(color.FgYellow, color.Bold)) URLs := ctx.Args() firstURL := URLs.Get(0) diff --git a/cmd/difference.go b/cmd/difference.go index a7f09530..3222ddbe 100644 --- a/cmd/difference.go +++ b/cmd/difference.go @@ -29,6 +29,7 @@ type differType int const ( differInNone differType = iota // does not differ differInSize // differs in size + differInTime // differs in time differInType // only in source differInFirst // only in target differInSecond // differs in type, exfile/directory @@ -40,6 +41,8 @@ func (d differType) String() string { return "" case differInSize: return "size" + case differInTime: + return "time" case differInType: return "type" case differInFirst: @@ -161,9 +164,10 @@ func difference(sourceClnt, targetClnt Client, sourceURL, targetURL string, isRe if normalizedExpected == normalizedCurrent { srcType, tgtType := srcCtnt.Type, tgtCtnt.Type srcSize, tgtSize := srcCtnt.Size, tgtCtnt.Size + srcTime, tgtTime := srcCtnt.Time, tgtCtnt.Time if srcType.IsRegular() && !tgtType.IsRegular() || !srcType.IsRegular() && tgtType.IsRegular() { - // Type differes. Source is never a directory. + // Type differs. Source is never a directory. diffCh <- diffMessage{ FirstURL: srcCtnt.URL.String(), SecondURL: tgtCtnt.URL.String(), @@ -171,7 +175,9 @@ func difference(sourceClnt, targetClnt Client, sourceURL, targetURL string, isRe firstContent: srcCtnt, secondContent: tgtCtnt, } - } else if (srcType.IsRegular() && tgtType.IsRegular()) && srcSize != tgtSize { + continue + } + if (srcType.IsRegular() && tgtType.IsRegular()) && srcSize != tgtSize { // Regular files differing in size. diffCh <- diffMessage{ FirstURL: srcCtnt.URL.String(), @@ -180,6 +186,15 @@ func difference(sourceClnt, targetClnt Client, sourceURL, targetURL string, isRe firstContent: srcCtnt, secondContent: tgtCtnt, } + } else if srcTime.After(tgtTime) { + // Regular files differing in timestamp. + diffCh <- diffMessage{ + FirstURL: srcCtnt.URL.String(), + SecondURL: tgtCtnt.URL.String(), + Diff: differInTime, + firstContent: srcCtnt, + secondContent: tgtCtnt, + } } // No differ if returnSimilar { diff --git a/cmd/mirror-url.go b/cmd/mirror-url.go index 6a6e096b..e591fc29 100644 --- a/cmd/mirror-url.go +++ b/cmd/mirror-url.go @@ -108,7 +108,7 @@ func deltaSourceTarget(sourceURL string, targetURL string, isForce bool, isFake case differInType: URLsCh <- URLs{Error: errInvalidTarget(diffMsg.SecondURL)} continue - case differInSize: + case differInSize, differInTime: if !isForce && !isFake { // Size differs and force not set URLsCh <- URLs{Error: errOverWriteNotAllowed(diffMsg.SecondURL)}