From 3e5178ddb29ba839d45810fb46ab69ce3b772d32 Mon Sep 17 00:00:00 2001 From: Harshavardhana Date: Mon, 24 Aug 2015 00:02:50 -0700 Subject: [PATCH] diff should handle type and size as well in recursive diff --- config-main.go | 2 +- diff.go | 60 +++++++++++++++++++++++++++++++++++++++++++++----- ls.go | 2 +- mirror-url.go | 2 +- session-v2.go | 2 +- 5 files changed, 59 insertions(+), 9 deletions(-) diff --git a/config-main.go b/config-main.go index 784e80e0..066967d5 100644 --- a/config-main.go +++ b/config-main.go @@ -73,7 +73,7 @@ func (a AliasMessage) String() string { return message } jsonMessageBytes, e := json.Marshal(a) - fatalIf(probe.NewError(e), "Unable to marshall into JSON.") + fatalIf(probe.NewError(e), "Unable to marshal into JSON.") return string(jsonMessageBytes) } diff --git a/diff.go b/diff.go index a109c2f1..0c3a72bf 100644 --- a/diff.go +++ b/diff.go @@ -19,6 +19,7 @@ package main import ( "encoding/json" "errors" + "os" "path/filepath" "strings" "sync" @@ -281,6 +282,11 @@ func dodiffRecursive(firstClnt, secondClnt client.Client, ch chan diff) { secondTrie := patricia.NewTrie() wg := new(sync.WaitGroup) + type urlAttr struct { + Size int64 + Type os.FileMode + } + wg.Add(1) go func(ch chan<- diff) { defer wg.Done() @@ -292,7 +298,7 @@ func dodiffRecursive(firstClnt, secondClnt client.Client, ch chan diff) { } return } - firstTrie.Insert(patricia.Prefix(firstContentCh.Content.Name), struct{}{}) + firstTrie.Insert(patricia.Prefix(firstContentCh.Content.Name), urlAttr{firstContentCh.Content.Size, firstContentCh.Content.Type}) } }(ch) wg.Add(1) @@ -306,7 +312,7 @@ func dodiffRecursive(firstClnt, secondClnt client.Client, ch chan diff) { } return } - secondTrie.Insert(patricia.Prefix(secondContentCh.Content.Name), struct{}{}) + secondTrie.Insert(patricia.Prefix(secondContentCh.Content.Name), urlAttr{secondContentCh.Content.Size, secondContentCh.Content.Type}) } }(ch) @@ -341,17 +347,61 @@ func dodiffRecursive(firstClnt, secondClnt client.Client, ch chan diff) { defer close(matchNameCh) }(matchNameCh) for matchName := range matchNameCh { + firstURLDelimited := firstClnt.URL().String()[:strings.LastIndex(firstClnt.URL().String(), string(firstClnt.URL().Separator))+1] + secondURLDelimited := secondClnt.URL().String()[:strings.LastIndex(secondClnt.URL().String(), string(secondClnt.URL().Separator))+1] + firstURL := firstURLDelimited + matchName + secondURL := secondURLDelimited + matchName if !secondTrie.Match(patricia.Prefix(matchName)) { - firstURLDelimited := firstClnt.URL().String()[:strings.LastIndex(firstClnt.URL().String(), string(firstClnt.URL().Separator))+1] - firstURL := firstURLDelimited + matchName ch <- diff{ message: DiffMessage{ FirstURL: firstURL, - SecondURL: firstClnt.URL().String(), + SecondURL: secondURL, Diff: "only-in-first", }.String(), err: nil, } + } else { + firstURLAttr := firstTrie.Get(patricia.Prefix(matchName)).(urlAttr) + secondURLAttr := secondTrie.Get(patricia.Prefix(matchName)).(urlAttr) + + if firstURLAttr.Type.IsRegular() { + if !secondURLAttr.Type.IsRegular() { + ch <- diff{ + message: DiffMessage{ + FirstURL: firstURL, + SecondURL: secondURL, + Diff: "type", + }.String(), + err: nil, + } + continue + } + } + + if firstURLAttr.Type.IsDir() { + if !secondURLAttr.Type.IsDir() { + ch <- diff{ + message: DiffMessage{ + FirstURL: firstURL, + SecondURL: secondURL, + Diff: "type", + }.String(), + err: nil, + } + continue + } + } + + if firstURLAttr.Size != secondURLAttr.Size { + ch <- diff{ + message: DiffMessage{ + FirstURL: firstURL, + SecondURL: secondURL, + Diff: "size", + }.String(), + err: nil, + } + } } } } diff --git a/ls.go b/ls.go index f9a4fec5..656a1d75 100644 --- a/ls.go +++ b/ls.go @@ -58,7 +58,7 @@ func (c ContentMessage) String() string { return message } jsonMessageBytes, e := json.Marshal(c) - fatalIf(probe.NewError(e), "Unable to marshall into JSON.") + fatalIf(probe.NewError(e), "Unable to marshal into JSON.") return string(jsonMessageBytes) } diff --git a/mirror-url.go b/mirror-url.go index 31ff2e58..b96bbbdb 100644 --- a/mirror-url.go +++ b/mirror-url.go @@ -58,7 +58,7 @@ func (s MirrorMessage) String() string { return fmt.Sprintf("ā€˜%s’ -> ā€˜%s’", s.Source, s.Targets) } mirrorMessageBytes, e := json.Marshal(s) - fatalIf(probe.NewError(e), "Unable to marshall into JSON.") + fatalIf(probe.NewError(e), "Unable to marshal into JSON.") return string(mirrorMessageBytes) } diff --git a/session-v2.go b/session-v2.go index a256c06f..3af6ae83 100644 --- a/session-v2.go +++ b/session-v2.go @@ -87,7 +87,7 @@ func (s sessionV2) String() string { CommandArgs: s.Header.CommandArgs, } sessionJSONBytes, e := json.Marshal(sessionMesage) - fatalIf(probe.NewError(e), "Unable to marshall into JSON.") + fatalIf(probe.NewError(e), "Unable to marshal into JSON.") return string(sessionJSONBytes) }