diff --git a/diff-main.go b/diff-main.go index 13dd0893..f7a70450 100644 --- a/diff-main.go +++ b/diff-main.go @@ -124,6 +124,7 @@ func checkDiffSyntax(ctx *cli.Context) { if err != nil { fatalIf(err.Trace(firstURL), fmt.Sprintf("Unable to stat '%s'.", firstURL)) } + // Verify if its a directory. if !firstContent.Type.IsDir() { fatalIf(errInvalidArgument().Trace(firstURL), fmt.Sprintf("ā€˜%s’ is not a folder.", firstURL)) @@ -134,6 +135,7 @@ func checkDiffSyntax(ctx *cli.Context) { if err != nil { fatalIf(err.Trace(secondURL), fmt.Sprintf("Unable to stat '%s'.", secondURL)) } + // Verify if its a directory. if !secondContent.Type.IsDir() { fatalIf(errInvalidArgument().Trace(secondURL), fmt.Sprintf("ā€˜%s’ is not a folder.", secondURL)) @@ -161,40 +163,49 @@ func doDiffMain(firstURL, secondURL string) { fatalIf(err.Trace(firstAlias, firstURL, secondAlias, secondURL), fmt.Sprintf("Failed to diff '%s' and '%s'", firstURL, secondURL)) } - difference, err := objectDifferenceFactory(secondAlias, secondURL) + + secondClient, err := newClientFromAlias(secondAlias, secondURL) if err != nil { fatalIf(err.Trace(firstAlias, firstURL, secondAlias, secondURL), fmt.Sprintf("Failed to diff '%s' and '%s'", firstURL, secondURL)) } - isRecursive := true - isIncomplete := false - for sourceContent := range firstClient.List(isRecursive, isIncomplete) { - if sourceContent.Err != nil { - switch sourceContent.Err.ToGoError().(type) { + + // 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(sourceContent.Err.Trace(firstURL, secondURL), fmt.Sprintf("Failed on '%s'", firstURL)) + errorIf(firstContent.Err.Trace(firstURL, secondURL), fmt.Sprintf("Failed on '%s'", firstURL)) default: - fatalIf(sourceContent.Err.Trace(firstURL, secondURL), fmt.Sprintf("Failed on '%s'", firstURL)) + fatalIf(firstContent.Err.Trace(firstURL, secondURL), fmt.Sprintf("Failed on '%s'", firstURL)) } continue } - if sourceContent.Type.IsDir() { + if firstContent.Type.IsDir() { continue } - suffix := strings.TrimPrefix(sourceContent.URL.String(), firstURL) - differ, err := difference(suffix, sourceContent.Type, sourceContent.Size) + firstSuffix := strings.TrimPrefix(firstContent.URL.String(), firstURL) + differ, err := difference(secondURL, firstSuffix, firstContent.Type, firstContent.Size) if err != nil { - errorIf(sourceContent.Err.Trace(secondURL, suffix), - fmt.Sprintf("Failed on '%s'", urlJoinPath(secondURL, suffix))) + errorIf(firstContent.Err.Trace(secondURL, firstSuffix), + fmt.Sprintf("Failed on '%s'", urlJoinPath(secondURL, firstSuffix))) continue } if differ == differNone { continue } printMsg(diffMessage{ - FirstURL: sourceContent.URL.String(), - SecondURL: urlJoinPath(secondURL, suffix), + FirstURL: firstContent.URL.String(), + SecondURL: urlJoinPath(secondURL, firstSuffix), Diff: differ, }) } diff --git a/difference.go b/difference.go index c96a043f..3fc8082c 100644 --- a/difference.go +++ b/difference.go @@ -22,10 +22,10 @@ import ( "github.com/minio/minio/pkg/probe" ) -// 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, os.FileMode, int64) (string, *probe.Error) +// 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) const ( differSize string = "size" // differs in size @@ -37,24 +37,20 @@ const ( // objectDifferenceFactory returns objectDifference function to check for difference // between sourceURL and targetURL // for usage reference check diff and mirror commands -func objectDifferenceFactory(targetAlias, targetURL string) (objectDifference, *probe.Error) { - clnt, err := newClientFromAlias(targetAlias, targetURL) - if err != nil { - return nil, err.Trace(targetAlias, targetURL) - } +func objectDifferenceFactory(targetClnt Client) objectDifference { isIncomplete := false isRecursive := true - ch := clnt.List(isRecursive, isIncomplete) - current := targetURL + ch := targetClnt.List(isRecursive, isIncomplete) reachedEOF := false ok := false var content *clientContent - difference := func(suffix string, srcType os.FileMode, srcSize int64) (string, *probe.Error) { + return func(targetURL string, suffix string, srcType os.FileMode, srcSize int64) (string, *probe.Error) { if reachedEOF { // would mean the suffix is not on target return differOnlyFirst, nil } + current := targetURL expected := urlJoinPath(targetURL, suffix) for { if expected < current { @@ -84,5 +80,4 @@ func objectDifferenceFactory(targetAlias, targetURL string) (objectDifference, * current = content.URL.String() } } - return difference, nil } diff --git a/mirror-main.go b/mirror-main.go index d0d51ee5..3e6120f6 100644 --- a/mirror-main.go +++ b/mirror-main.go @@ -294,8 +294,7 @@ func doMirrorSession(session *sessionV6) { // For all non critical errors we can continue for the // remaining files. switch sURLs.Error.ToGoError().(type) { - // Handle this specifically for filesystem related - // errors. + // Handle this specifically for filesystem related errors. case BrokenSymlink: continue case TooManyLevelsSymlink: @@ -311,7 +310,7 @@ func doMirrorSession(session *sessionV6) { } // For critical errors we should exit. Session // can be resumed after the user figures out - // the problem. + // the problem. session.CloseAndDie() } } diff --git a/mirror-url.go b/mirror-url.go index b2632d6e..b0ceb3d5 100644 --- a/mirror-url.go +++ b/mirror-url.go @@ -107,19 +107,27 @@ func deltaSourceTargets(sourceURL string, targetURL string, isForce bool, mirror defer close(mirrorURLsCh) - objectDifferenceTarget, err := objectDifferenceFactory(targetAlias, targetURL) - if err != nil { - mirrorURLsCh <- mirrorURLs{Error: err.Trace(targetAlias, targetURL)} - return - } - sourceClient, err := newClientFromAlias(sourceAlias, sourceURL) if err != nil { mirrorURLsCh <- mirrorURLs{Error: err.Trace(sourceAlias, sourceURL)} return } - for sourceContent := range sourceClient.List(true, false) { + targetClnt, err := newClientFromAlias(targetAlias, targetURL) + if err != nil { + mirrorURLsCh <- mirrorURLs{Error: err.Trace(targetAlias, targetURL)} + 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()), @@ -129,8 +137,8 @@ func deltaSourceTargets(sourceURL string, targetURL string, isForce bool, mirror if sourceContent.Type.IsDir() { continue } - suffix := strings.TrimPrefix(sourceContent.URL.String(), sourceURL) - differ, err := objectDifferenceTarget(suffix, sourceContent.Type, sourceContent.Size) + sourceSuffix := strings.TrimPrefix(sourceContent.URL.String(), sourceURL) + differ, err := objectDifferenceTarget(targetURL, sourceSuffix, sourceContent.Type, sourceContent.Size) if err != nil { mirrorURLsCh <- mirrorURLs{Error: err.Trace(sourceContent.URL.String())} continue @@ -140,7 +148,7 @@ func deltaSourceTargets(sourceURL string, targetURL string, isForce bool, mirror continue } if differ == differType { - mirrorURLsCh <- mirrorURLs{Error: errInvalidTarget(suffix)} + mirrorURLsCh <- mirrorURLs{Error: errInvalidTarget(sourceSuffix)} continue } if differ == differSize && !isForce { @@ -149,7 +157,7 @@ func deltaSourceTargets(sourceURL string, targetURL string, isForce bool, mirror continue } // either available only in source or size differs and force is set - targetPath := urlJoinPath(targetURL, suffix) + targetPath := urlJoinPath(targetURL, sourceSuffix) targetContent := &clientContent{URL: *newURL(targetPath)} mirrorURLsCh <- mirrorURLs{ SourceAlias: sourceAlias,