diff: Implement url difference with time.
Also make a 'differType' - fixes #1632
This commit is contained in:
+11
-7
@@ -70,7 +70,7 @@ type diffMessage struct {
|
||||
Status string `json:"status"`
|
||||
FirstURL string `json:"first"`
|
||||
SecondURL string `json:"second"`
|
||||
Diff string `json:"diff"`
|
||||
Diff differType `json:"diff"`
|
||||
Error *probe.Error `json:"error,omitempty"`
|
||||
}
|
||||
|
||||
@@ -78,15 +78,18 @@ type diffMessage struct {
|
||||
func (d diffMessage) String() string {
|
||||
msg := ""
|
||||
switch d.Diff {
|
||||
case "only-in-first":
|
||||
case differInFirst:
|
||||
msg = console.Colorize("DiffMessage",
|
||||
"‘"+d.FirstURL+"’"+" and "+"‘"+d.SecondURL+"’") + console.Colorize("DiffOnlyInFirst", " - only in first.")
|
||||
case "type":
|
||||
case differInType:
|
||||
msg = console.Colorize("DiffMessage",
|
||||
"‘"+d.FirstURL+"’"+" and "+"‘"+d.SecondURL+"’") + console.Colorize("DiffType", " - differ in type.")
|
||||
case "size":
|
||||
case differInSize:
|
||||
msg = console.Colorize("DiffMessage",
|
||||
"‘"+d.FirstURL+"’"+" and "+"‘"+d.SecondURL+"’") + console.Colorize("DiffSize", " - differ in size.")
|
||||
case differInTime:
|
||||
msg = console.Colorize("DiffMessage",
|
||||
"‘"+d.FirstURL+"’"+" and "+"‘"+d.SecondURL+"’") + console.Colorize("DiffTime", " - differ in modified time.")
|
||||
default:
|
||||
fatalIf(errDummy().Trace(d.FirstURL, d.SecondURL),
|
||||
"Unhandled difference between ‘"+d.FirstURL+"’ and ‘"+d.SecondURL+"’.")
|
||||
@@ -100,7 +103,7 @@ func (d diffMessage) JSON() string {
|
||||
d.Status = "success"
|
||||
diffJSONBytes, e := json.Marshal(d)
|
||||
fatalIf(probe.NewError(e),
|
||||
"Unable to marshal diff message ‘"+d.FirstURL+"’, ‘"+d.SecondURL+"’ and ‘"+d.Diff+"’.")
|
||||
"Unable to marshal diff message ‘"+d.FirstURL+"’, ‘"+d.SecondURL+"’ and ‘"+string(d.Diff)+"’.")
|
||||
return string(diffJSONBytes)
|
||||
}
|
||||
|
||||
@@ -194,13 +197,13 @@ func doDiffMain(firstURL, secondURL string) {
|
||||
continue
|
||||
}
|
||||
firstSuffix := strings.TrimPrefix(firstContent.URL.String(), firstURL)
|
||||
differ, err := difference(secondURL, firstSuffix, firstContent.Type, firstContent.Size)
|
||||
differ, err := difference(secondURL, firstSuffix, firstContent.Type, firstContent.Size, firstContent.Time)
|
||||
if err != nil {
|
||||
errorIf(firstContent.Err.Trace(secondURL, firstSuffix),
|
||||
fmt.Sprintf("Failed on '%s'", urlJoinPath(secondURL, firstSuffix)))
|
||||
continue
|
||||
}
|
||||
if differ == differNone {
|
||||
if differ == differInNone {
|
||||
continue
|
||||
}
|
||||
printMsg(diffMessage{
|
||||
@@ -224,6 +227,7 @@ func mainDiff(ctx *cli.Context) {
|
||||
console.SetColor("DiffOnlyInFirst", color.New(color.FgRed, color.Bold))
|
||||
console.SetColor("DiffType", color.New(color.FgYellow, color.Bold))
|
||||
console.SetColor("DiffSize", color.New(color.FgMagenta, color.Bold))
|
||||
console.SetColor("DiffTime", color.New(color.FgYellow, color.Bold))
|
||||
|
||||
URLs := ctx.Args()
|
||||
firstURL := URLs[0]
|
||||
|
||||
+27
-18
@@ -18,6 +18,7 @@ package main
|
||||
|
||||
import (
|
||||
"os"
|
||||
"time"
|
||||
|
||||
"github.com/minio/minio/pkg/probe"
|
||||
)
|
||||
@@ -25,18 +26,21 @@ import (
|
||||
// objectDifference function finds the difference between object on
|
||||
// source and target it takes suffix string, type and size on the
|
||||
// source objectDifferenceFactory returns objectDifference function
|
||||
type objectDifference func(string, string, os.FileMode, int64) (string, *probe.Error)
|
||||
type objectDifference func(string, string, os.FileMode, int64, time.Time) (differType, *probe.Error)
|
||||
|
||||
// differType difference in type.
|
||||
type differType string
|
||||
|
||||
const (
|
||||
differSize string = "size" // differs in size
|
||||
differOnlyFirst string = "only-in-first" // only on source
|
||||
differType string = "type" // differs in type, ex file/directory
|
||||
differNone string = "" // does not differ
|
||||
differInSize differType = "size" // differs in size
|
||||
differInTime = "time" // differs in time
|
||||
differInFirst = "only-in-first" // only on first source
|
||||
differInType = "type" // differs in type, exfile/directory
|
||||
differInNone = "" // does not differ
|
||||
)
|
||||
|
||||
// objectDifferenceFactory returns objectDifference function to check for difference
|
||||
// between sourceURL and targetURL
|
||||
// for usage reference check diff and mirror commands
|
||||
// between sourceURL and targetURL, for usage reference check diff and mirror commands.
|
||||
func objectDifferenceFactory(targetClnt Client) objectDifference {
|
||||
isIncomplete := false
|
||||
isRecursive := true
|
||||
@@ -45,34 +49,39 @@ func objectDifferenceFactory(targetClnt Client) objectDifference {
|
||||
ok := false
|
||||
var content *clientContent
|
||||
|
||||
return func(targetURL string, suffix string, srcType os.FileMode, srcSize int64) (string, *probe.Error) {
|
||||
return func(targetURL string, srcSuffix string, srcType os.FileMode, srcSize int64, srcTime time.Time) (differType, *probe.Error) {
|
||||
if reachedEOF {
|
||||
// would mean the suffix is not on target
|
||||
return differOnlyFirst, nil
|
||||
// Would mean the suffix is not on target.
|
||||
return differInFirst, nil
|
||||
}
|
||||
current := targetURL
|
||||
expected := urlJoinPath(targetURL, suffix)
|
||||
expected := urlJoinPath(targetURL, srcSuffix)
|
||||
for {
|
||||
if expected < current {
|
||||
return differOnlyFirst, nil // not available in the target
|
||||
return differInFirst, nil // Not available in the target.
|
||||
}
|
||||
if expected == current {
|
||||
tgtType := content.Type
|
||||
tgtSize := content.Size
|
||||
tgtTime := content.Time
|
||||
if srcType.IsRegular() && !tgtType.IsRegular() {
|
||||
// Type differes. Source is never a directory
|
||||
return differType, nil
|
||||
// Type differes. Source is never a directory.
|
||||
return differInType, nil
|
||||
}
|
||||
if (srcType.IsRegular() && tgtType.IsRegular()) && srcSize != tgtSize {
|
||||
// regular files differing in size
|
||||
return differSize, nil
|
||||
// Regular files differing in size.
|
||||
return differInSize, nil
|
||||
}
|
||||
return differNone, nil // available in the target
|
||||
if (srcType.IsRegular() && tgtType.IsRegular()) && srcTime.After(tgtTime) {
|
||||
// Regular files differing in time.
|
||||
return differInTime, nil
|
||||
}
|
||||
return differInNone, nil // Available in the target.
|
||||
}
|
||||
content, ok = <-ch
|
||||
if !ok {
|
||||
reachedEOF = true
|
||||
return differOnlyFirst, nil
|
||||
return differInFirst, nil
|
||||
}
|
||||
if content.Err != nil {
|
||||
return "", content.Err.Trace()
|
||||
|
||||
+5
-5
@@ -138,20 +138,20 @@ func deltaSourceTargets(sourceURL string, targetURL string, isForce bool, isFake
|
||||
continue
|
||||
}
|
||||
sourceSuffix := strings.TrimPrefix(sourceContent.URL.String(), sourceURL)
|
||||
differ, err := objectDifferenceTarget(targetURL, sourceSuffix, sourceContent.Type, sourceContent.Size)
|
||||
differ, err := objectDifferenceTarget(targetURL, sourceSuffix, sourceContent.Type, sourceContent.Size, sourceContent.Time)
|
||||
if err != nil {
|
||||
mirrorURLsCh <- mirrorURLs{Error: err.Trace(sourceContent.URL.String())}
|
||||
continue
|
||||
}
|
||||
if differ == differNone {
|
||||
// no difference, continue
|
||||
if differ == differInNone {
|
||||
// No difference, continue.
|
||||
continue
|
||||
}
|
||||
if differ == differType {
|
||||
if differ == differInType {
|
||||
mirrorURLsCh <- mirrorURLs{Error: errInvalidTarget(sourceSuffix)}
|
||||
continue
|
||||
}
|
||||
if differ == differSize && !isForce && !isFake {
|
||||
if differ == differInSize && !isForce && !isFake {
|
||||
// Size differs and force not set
|
||||
mirrorURLsCh <- mirrorURLs{Error: errOverWriteNotAllowed(sourceContent.URL.String())}
|
||||
continue
|
||||
|
||||
Reference in New Issue
Block a user