From b5da4e98571b02dc106de4f9b2cb2a298489f1b1 Mon Sep 17 00:00:00 2001 From: Alberto Cortés Date: Wed, 22 Feb 2017 16:45:46 +0100 Subject: Fix issue 275 (edited) (#276) Fix #275 . It was not possible to write a test for this issue as the original fsnoder didn't support filenames with length > 1. Therefore this patch has 3 commits: add support for long filenames in fsnoder. add a test case for the issue using the new long filenames from step 1. fix the issue by comparing paths level by level instead of lexigographically over the whole path. --- utils/merkletrie/noder/path.go | 31 ++++++++++++++++++++++++++++++- 1 file changed, 30 insertions(+), 1 deletion(-) (limited to 'utils/merkletrie/noder/path.go') diff --git a/utils/merkletrie/noder/path.go b/utils/merkletrie/noder/path.go index c992f7e..85742db 100644 --- a/utils/merkletrie/noder/path.go +++ b/utils/merkletrie/noder/path.go @@ -1,6 +1,9 @@ package noder -import "bytes" +import ( + "bytes" + "strings" +) // Path values represent a noder and its ancestors. The root goes first // and the actual final noder the path is refering to will be the last. @@ -57,3 +60,29 @@ func (p Path) Children() ([]Noder, error) { func (p Path) NumChildren() (int, error) { return p.Last().NumChildren() } + +// Compare returns -1, 0 or 1 if the path p is smaller, equal or bigger +// than other, in "directory order"; for example: +// +// "a" < "b" +// "a/b/c/d/z" < "b" +// "a/b/a" > "a/b" +func (p Path) Compare(other Path) int { + i := 0 + for { + switch { + case len(other) == len(p) && i == len(p): + return 0 + case i == len(other): + return 1 + case i == len(p): + return -1 + default: + cmp := strings.Compare(p[i].Name(), other[i].Name()) + if cmp != 0 { + return cmp + } + } + i++ + } +} -- cgit