Rewrite diff code (#1681)
This commit is contained in:
committed by
Harshavardhana
parent
65a31e34c9
commit
98ac1817cb
+1
-1
@@ -743,7 +743,7 @@ func (f *fsClient) listRecursiveInRoutine(contentCh chan *clientContent, incompl
|
||||
return e
|
||||
}
|
||||
}
|
||||
if fi.Mode().IsRegular() || fi.Mode().IsDir() {
|
||||
if fi.Mode().IsRegular() {
|
||||
if incomplete {
|
||||
if !strings.HasSuffix(fi.Name(), partSuffix) {
|
||||
return nil
|
||||
|
||||
+14
-47
@@ -67,11 +67,13 @@ EXAMPLES:
|
||||
|
||||
// diffMessage json container for diff messages
|
||||
type diffMessage struct {
|
||||
Status string `json:"status"`
|
||||
FirstURL string `json:"first"`
|
||||
SecondURL string `json:"second"`
|
||||
Diff differType `json:"diff"`
|
||||
Error *probe.Error `json:"error,omitempty"`
|
||||
Status string `json:"status"`
|
||||
FirstURL string `json:"first"`
|
||||
SecondURL string `json:"second"`
|
||||
Diff differType `json:"diff"`
|
||||
Error *probe.Error `json:"error,omitempty"`
|
||||
firstContent *clientContent
|
||||
secondContent *clientContent
|
||||
}
|
||||
|
||||
// String colorized diff message
|
||||
@@ -80,16 +82,16 @@ func (d diffMessage) String() string {
|
||||
switch d.Diff {
|
||||
case differInFirst:
|
||||
msg = console.Colorize("DiffMessage",
|
||||
"‘"+d.FirstURL+"’"+" and "+"‘"+d.SecondURL+"’") + console.Colorize("DiffOnlyInFirst", " - only in first.")
|
||||
"‘"+d.FirstURL+"’") + console.Colorize("DiffOnlyInFirst", " - only in first.")
|
||||
case differInSecond:
|
||||
msg = console.Colorize("DiffMessage",
|
||||
"‘"+d.SecondURL+"’") + console.Colorize("DiffOnlyInSecond", " - only in second.")
|
||||
case differInType:
|
||||
msg = console.Colorize("DiffMessage",
|
||||
"‘"+d.FirstURL+"’"+" and "+"‘"+d.SecondURL+"’") + console.Colorize("DiffType", " - differ in type.")
|
||||
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+"’.")
|
||||
@@ -173,44 +175,9 @@ func doDiffMain(firstURL, secondURL string) {
|
||||
fmt.Sprintf("Failed to diff '%s' and '%s'", firstURL, secondURL))
|
||||
}
|
||||
|
||||
// Setup object difference function.
|
||||
difference := objectDifferenceFactory(secondClient)
|
||||
|
||||
// Set default values for listing.
|
||||
isRecursive := true // recursive is always true for diff.
|
||||
isIncomplete := false // we will not compare any incomplete objects.
|
||||
|
||||
// List all the elements on first URL and compare with
|
||||
// 'difference' function.
|
||||
for firstContent := range firstClient.List(isRecursive, isIncomplete) {
|
||||
if firstContent.Err != nil {
|
||||
switch firstContent.Err.ToGoError().(type) {
|
||||
// Handle this specifically for filesystem related errors.
|
||||
case BrokenSymlink, TooManyLevelsSymlink, PathNotFound, PathInsufficientPermission:
|
||||
errorIf(firstContent.Err.Trace(firstURL, secondURL), fmt.Sprintf("Failed on '%s'", firstURL))
|
||||
default:
|
||||
fatalIf(firstContent.Err.Trace(firstURL, secondURL), fmt.Sprintf("Failed on '%s'", firstURL))
|
||||
}
|
||||
continue
|
||||
}
|
||||
if firstContent.Type.IsDir() {
|
||||
continue
|
||||
}
|
||||
firstSuffix := strings.TrimPrefix(firstContent.URL.String(), firstURL)
|
||||
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 == differInNone {
|
||||
continue
|
||||
}
|
||||
printMsg(diffMessage{
|
||||
FirstURL: firstContent.URL.String(),
|
||||
SecondURL: urlJoinPath(secondURL, firstSuffix),
|
||||
Diff: differ,
|
||||
})
|
||||
// Diff first and second urls.
|
||||
for diffMsg := range objectDifference(firstClient, secondClient, firstURL, secondURL) {
|
||||
printMsg(diffMsg)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
+148
-55
@@ -17,76 +17,169 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"os"
|
||||
"time"
|
||||
|
||||
"github.com/minio/minio/pkg/probe"
|
||||
"fmt"
|
||||
"strings"
|
||||
)
|
||||
|
||||
// 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, time.Time) (differType, *probe.Error)
|
||||
|
||||
// differType difference in type.
|
||||
type differType string
|
||||
type differType int
|
||||
|
||||
const (
|
||||
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
|
||||
differInNone differType = iota // does not differ
|
||||
differInSize // differs in size
|
||||
differInType // only in source
|
||||
differInFirst // only in target
|
||||
differInSecond // differs in type, exfile/directory
|
||||
)
|
||||
|
||||
// objectDifferenceFactory returns objectDifference function to check for difference
|
||||
// between sourceURL and targetURL, for usage reference check diff and mirror commands.
|
||||
func objectDifferenceFactory(targetClnt Client) objectDifference {
|
||||
isIncomplete := false
|
||||
isRecursive := true
|
||||
ch := targetClnt.List(isRecursive, isIncomplete)
|
||||
reachedEOF := false
|
||||
ok := false
|
||||
var content *clientContent
|
||||
func (d differType) String() string {
|
||||
switch d {
|
||||
case differInNone:
|
||||
return ""
|
||||
case differInSize:
|
||||
return "size"
|
||||
case differInType:
|
||||
return "type"
|
||||
case differInFirst:
|
||||
return "only-in-first"
|
||||
case differInSecond:
|
||||
return "only-in-second"
|
||||
}
|
||||
return "unknown"
|
||||
}
|
||||
|
||||
// objectDifference function finds the difference between all objects
|
||||
// recursively in sorted order from source and target.
|
||||
func objectDifference(sourceClnt, targetClnt Client, sourceURL, targetURL string) (diffCh chan diffMessage) {
|
||||
var (
|
||||
srcEOF, tgtEOF bool
|
||||
srcOk, tgtOk bool
|
||||
srcCtnt, tgtCtnt *clientContent
|
||||
srcSuffix, tgtSuffix string
|
||||
)
|
||||
|
||||
// Set default values for listing.
|
||||
isRecursive := true // recursive is always true for diff.
|
||||
isIncomplete := false // we will not compare any incomplete objects.
|
||||
srcCh := sourceClnt.List(isRecursive, isIncomplete)
|
||||
tgtCh := targetClnt.List(isRecursive, isIncomplete)
|
||||
|
||||
diffCh = make(chan diffMessage, 1000)
|
||||
|
||||
go func() {
|
||||
|
||||
srcCtnt, srcOk = <-srcCh
|
||||
tgtCtnt, tgtOk = <-tgtCh
|
||||
|
||||
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 differInFirst, nil
|
||||
}
|
||||
current := targetURL
|
||||
expected := urlJoinPath(targetURL, srcSuffix)
|
||||
for {
|
||||
if expected < current {
|
||||
return differInFirst, nil // Not available in the target.
|
||||
srcEOF = !srcOk
|
||||
tgtEOF = !tgtOk
|
||||
|
||||
// No objects from source AND target: Finish
|
||||
if srcEOF && tgtEOF {
|
||||
close(diffCh)
|
||||
break
|
||||
}
|
||||
|
||||
if !srcEOF && srcCtnt.Err != nil {
|
||||
switch srcCtnt.Err.ToGoError().(type) {
|
||||
// Handle this specifically for filesystem related errors.
|
||||
case BrokenSymlink, TooManyLevelsSymlink, PathNotFound, PathInsufficientPermission:
|
||||
errorIf(srcCtnt.Err.Trace(sourceURL, targetURL), fmt.Sprintf("Failed on '%s'", sourceURL))
|
||||
default:
|
||||
fatalIf(srcCtnt.Err.Trace(sourceURL, targetURL), fmt.Sprintf("Failed on '%s'", sourceURL))
|
||||
}
|
||||
srcCtnt, srcOk = <-srcCh
|
||||
continue
|
||||
}
|
||||
|
||||
if !tgtEOF && tgtCtnt.Err != nil {
|
||||
switch tgtCtnt.Err.ToGoError().(type) {
|
||||
// Handle this specifically for filesystem related errors.
|
||||
case BrokenSymlink, TooManyLevelsSymlink, PathNotFound, PathInsufficientPermission:
|
||||
errorIf(tgtCtnt.Err.Trace(sourceURL, targetURL), fmt.Sprintf("Failed on '%s'", targetURL))
|
||||
default:
|
||||
fatalIf(tgtCtnt.Err.Trace(sourceURL, targetURL), fmt.Sprintf("Failed on '%s'", targetURL))
|
||||
}
|
||||
tgtCtnt, tgtOk = <-tgtCh
|
||||
continue
|
||||
}
|
||||
|
||||
// If source doesn't have objects anymore, comparison becomes obvious
|
||||
if srcEOF {
|
||||
diffCh <- diffMessage{
|
||||
SecondURL: tgtCtnt.URL.String(),
|
||||
Diff: differInSecond,
|
||||
secondContent: tgtCtnt,
|
||||
}
|
||||
tgtCtnt, tgtOk = <-tgtCh
|
||||
continue
|
||||
}
|
||||
|
||||
// The same for target
|
||||
if tgtEOF {
|
||||
diffCh <- diffMessage{
|
||||
FirstURL: srcCtnt.URL.String(),
|
||||
Diff: differInFirst,
|
||||
firstContent: srcCtnt,
|
||||
}
|
||||
srcCtnt, srcOk = <-srcCh
|
||||
continue
|
||||
}
|
||||
|
||||
srcSuffix = strings.TrimPrefix(srcCtnt.URL.String(), sourceURL)
|
||||
tgtSuffix = strings.TrimPrefix(tgtCtnt.URL.String(), targetURL)
|
||||
|
||||
current := urlJoinPath(targetURL, srcSuffix)
|
||||
expected := urlJoinPath(targetURL, tgtSuffix)
|
||||
|
||||
if expected > current {
|
||||
diffCh <- diffMessage{
|
||||
FirstURL: srcCtnt.URL.String(),
|
||||
Diff: differInFirst,
|
||||
firstContent: srcCtnt,
|
||||
}
|
||||
srcCtnt, srcOk = <-srcCh
|
||||
continue
|
||||
}
|
||||
if expected == current {
|
||||
tgtType := content.Type
|
||||
tgtSize := content.Size
|
||||
tgtTime := content.Time
|
||||
if srcType.IsRegular() && !tgtType.IsRegular() {
|
||||
srcType, tgtType := srcCtnt.Type, tgtCtnt.Type
|
||||
srcSize, tgtSize := srcCtnt.Size, tgtCtnt.Size
|
||||
if srcType.IsRegular() && !tgtType.IsRegular() ||
|
||||
!srcType.IsRegular() && tgtType.IsRegular() {
|
||||
// Type differes. Source is never a directory.
|
||||
return differInType, nil
|
||||
}
|
||||
if (srcType.IsRegular() && tgtType.IsRegular()) && srcSize != tgtSize {
|
||||
diffCh <- diffMessage{
|
||||
FirstURL: srcCtnt.URL.String(),
|
||||
SecondURL: tgtCtnt.URL.String(),
|
||||
Diff: differInType,
|
||||
firstContent: srcCtnt,
|
||||
secondContent: tgtCtnt,
|
||||
}
|
||||
} else if (srcType.IsRegular() && tgtType.IsRegular()) && srcSize != tgtSize {
|
||||
// Regular files differing in size.
|
||||
return differInSize, nil
|
||||
diffCh <- diffMessage{
|
||||
FirstURL: srcCtnt.URL.String(),
|
||||
SecondURL: tgtCtnt.URL.String(),
|
||||
Diff: differInSize,
|
||||
firstContent: srcCtnt,
|
||||
secondContent: tgtCtnt,
|
||||
}
|
||||
}
|
||||
if (srcType.IsRegular() && tgtType.IsRegular()) && srcTime.After(tgtTime) {
|
||||
// Regular files differing in time.
|
||||
return differInTime, nil
|
||||
}
|
||||
return differInNone, nil // Available in the target.
|
||||
// No differ
|
||||
srcCtnt, srcOk = <-srcCh
|
||||
tgtCtnt, tgtOk = <-tgtCh
|
||||
continue
|
||||
}
|
||||
content, ok = <-ch
|
||||
if !ok {
|
||||
reachedEOF = true
|
||||
return differInFirst, nil
|
||||
// Differ in second
|
||||
diffCh <- diffMessage{
|
||||
SecondURL: tgtCtnt.URL.String(),
|
||||
Diff: differInSecond,
|
||||
secondContent: tgtCtnt,
|
||||
}
|
||||
if content.Err != nil {
|
||||
return "", content.Err.Trace()
|
||||
}
|
||||
current = content.URL.String()
|
||||
tgtCtnt, tgtOk = <-tgtCh
|
||||
continue
|
||||
}
|
||||
}
|
||||
}()
|
||||
|
||||
return diffCh
|
||||
}
|
||||
|
||||
+39
-45
@@ -90,7 +90,7 @@ func checkMirrorSyntax(ctx *cli.Context) {
|
||||
}
|
||||
}
|
||||
|
||||
func deltaSourceTargets(sourceURL string, targetURL string, isForce bool, isFake bool, mirrorURLsCh chan<- mirrorURLs) {
|
||||
func deltaSourceTarget(sourceURL string, targetURL string, isForce bool, isFake bool, mirrorURLsCh chan<- mirrorURLs) {
|
||||
// source and targets are always directories
|
||||
sourceSeparator := string(newClientURL(sourceURL).Separator)
|
||||
if !strings.HasSuffix(sourceURL, sourceSeparator) {
|
||||
@@ -107,7 +107,7 @@ func deltaSourceTargets(sourceURL string, targetURL string, isForce bool, isFake
|
||||
|
||||
defer close(mirrorURLsCh)
|
||||
|
||||
sourceClient, err := newClientFromAlias(sourceAlias, sourceURL)
|
||||
sourceClnt, err := newClientFromAlias(sourceAlias, sourceURL)
|
||||
if err != nil {
|
||||
mirrorURLsCh <- mirrorURLs{Error: err.Trace(sourceAlias, sourceURL)}
|
||||
return
|
||||
@@ -119,57 +119,51 @@ func deltaSourceTargets(sourceURL string, targetURL string, isForce bool, isFake
|
||||
return
|
||||
}
|
||||
|
||||
// Setup object difference.
|
||||
objectDifferenceTarget := objectDifferenceFactory(targetClnt)
|
||||
|
||||
// Set default values for listing.
|
||||
isRecursive := true // recursive is always true for diff.
|
||||
isIncomplete := false // we will not compare any incomplete objects.
|
||||
|
||||
// List all the sources, compare using 'objectDifference' function.
|
||||
for sourceContent := range sourceClient.List(isRecursive, isIncomplete) {
|
||||
if sourceContent.Err != nil {
|
||||
mirrorURLsCh <- mirrorURLs{
|
||||
Error: sourceContent.Err.Trace(sourceClient.GetURL().String()),
|
||||
}
|
||||
continue
|
||||
}
|
||||
if sourceContent.Type.IsDir() {
|
||||
continue
|
||||
}
|
||||
sourceSuffix := strings.TrimPrefix(sourceContent.URL.String(), sourceURL)
|
||||
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 == differInNone {
|
||||
// List both source and target, compare and return values through channel.
|
||||
for diffMsg := range objectDifference(sourceClnt, targetClnt, sourceURL, targetURL) {
|
||||
if diffMsg.Diff == differInNone {
|
||||
// No difference, continue.
|
||||
continue
|
||||
}
|
||||
if differ == differInType {
|
||||
mirrorURLsCh <- mirrorURLs{Error: errInvalidTarget(sourceSuffix)}
|
||||
} else if diffMsg.Diff == differInType {
|
||||
mirrorURLsCh <- mirrorURLs{Error: errInvalidTarget(diffMsg.SecondURL)}
|
||||
continue
|
||||
}
|
||||
if differ == differInSize && !isForce && !isFake {
|
||||
// Size differs and force not set
|
||||
mirrorURLsCh <- mirrorURLs{Error: errOverWriteNotAllowed(sourceContent.URL.String())}
|
||||
} else if diffMsg.Diff == differInSize {
|
||||
if !isForce && !isFake {
|
||||
// Size differs and force not set
|
||||
mirrorURLsCh <- mirrorURLs{Error: errOverWriteNotAllowed(diffMsg.SecondURL)}
|
||||
continue
|
||||
}
|
||||
sourceSuffix := strings.TrimPrefix(diffMsg.FirstURL, sourceURL)
|
||||
// Either available only in source or size differs and force is set
|
||||
targetPath := urlJoinPath(targetURL, sourceSuffix)
|
||||
sourceContent := diffMsg.firstContent
|
||||
targetContent := &clientContent{URL: *newClientURL(targetPath)}
|
||||
mirrorURLsCh <- mirrorURLs{
|
||||
SourceAlias: sourceAlias,
|
||||
SourceContent: sourceContent,
|
||||
TargetAlias: targetAlias,
|
||||
TargetContent: targetContent,
|
||||
}
|
||||
continue
|
||||
}
|
||||
// either available only in source or size differs and force is set
|
||||
targetPath := urlJoinPath(targetURL, sourceSuffix)
|
||||
targetContent := &clientContent{URL: *newClientURL(targetPath)}
|
||||
mirrorURLsCh <- mirrorURLs{
|
||||
SourceAlias: sourceAlias,
|
||||
SourceContent: sourceContent,
|
||||
TargetAlias: targetAlias,
|
||||
TargetContent: targetContent,
|
||||
}
|
||||
} else if diffMsg.Diff == differInFirst {
|
||||
sourceSuffix := strings.TrimPrefix(diffMsg.FirstURL, sourceURL)
|
||||
// Either available only in source or size differs and force is set
|
||||
targetPath := urlJoinPath(targetURL, sourceSuffix)
|
||||
sourceContent := diffMsg.firstContent
|
||||
targetContent := &clientContent{URL: *newClientURL(targetPath)}
|
||||
mirrorURLsCh <- mirrorURLs{
|
||||
SourceAlias: sourceAlias,
|
||||
SourceContent: sourceContent,
|
||||
TargetAlias: targetAlias,
|
||||
TargetContent: targetContent,
|
||||
}
|
||||
} // else if diffMsg.Diff == differInSecond {
|
||||
// Add for bi-directional mirror.
|
||||
}
|
||||
}
|
||||
|
||||
func prepareMirrorURLs(sourceURL string, targetURL string, isForce bool, isFake bool) <-chan mirrorURLs {
|
||||
mirrorURLsCh := make(chan mirrorURLs)
|
||||
go deltaSourceTargets(sourceURL, targetURL, isForce, isFake, mirrorURLsCh)
|
||||
go deltaSourceTarget(sourceURL, targetURL, isForce, isFake, mirrorURLsCh)
|
||||
return mirrorURLsCh
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user