fixed getEntry, added regularSlashes()

now storing every path regularized in the manifest
This commit is contained in:
zsfelfoldi 2015-05-31 15:49:47 +02:00
parent 456cb39cf0
commit 017af45af5
3 changed files with 32 additions and 47 deletions

View file

@ -237,7 +237,7 @@ func (self *Api) Upload(lpath string) (string, error) {
if errors[i] != nil { if errors[i] != nil {
return "", errors[i] return "", errors[i]
} }
entry.Path = entry.Path[start:] entry.Path = regularSlashes(entry.Path[start:])
trie.addEntry(entry) trie.addEntry(entry)
} }
@ -315,7 +315,7 @@ func (self *Api) getPath(uri string) (reader SectionReader, mimeType string, sta
trie, err := loadManifest(self.dpa, key) trie, err := loadManifest(self.dpa, key)
if err != nil { if err != nil {
dpaLogger.Debugf("Swarm: loadManifest error: %v", err) dpaLogger.Debugf("Swarm: loadManifestTrie error: %v", err)
return return
} }

View file

@ -220,7 +220,7 @@ func (self *manifestTrie) findPrefixOf(path string) (entry *manifestTrieEntry, p
b := byte(path[0]) b := byte(path[0])
entry = self.entries[b] entry = self.entries[b]
if entry == nil { if entry == nil {
return nil, 0 return self.entries[256], 0
} }
epl := len(entry.Path) epl := len(entry.Path)
dpaLogger.Debugf("path = %v entry.Path = %v epl = %v", path, entry.Path, epl) dpaLogger.Debugf("path = %v entry.Path = %v epl = %v", path, entry.Path, epl)
@ -243,33 +243,27 @@ func (self *manifestTrie) findPrefixOf(path string) (entry *manifestTrieEntry, p
return return
} }
func (self *manifestTrie) getEntryNLS(path string) (entry *manifestTrieEntry, pos int) { // file system manifest always contains regularized paths
entry, pos = self.findPrefixOf(path) // no leading or trailing slashes, only single slashes inside
if entry != nil { func regularSlashes(path string) (res string) {
for (pos < len(path)) && (path[pos] == '/') { for i := 0; i < len(path); i++ {
pos++ if (path[i] != '/') || ((i > 0) && (path[i-1] != '/')) {
} res = res + path[i:i+1]
if (pos < len(path)) && (pos > 0) && (path[pos-1] != '/') {
return nil, 0
} }
} }
if (len(res) > 0) && (res[len(res)-1] == '/') {
res = res[:len(res)-1]
}
//fmt.Printf("%v -> %v\n", path, res)
return return
} }
func (self *manifestTrie) getEntry(path string) (entry *manifestTrieEntry, pos int) { func (self *manifestTrie) getEntry(spath string) (entry *manifestTrieEntry, fullpath string) {
var slash string path := regularSlashes(spath)
for { var pos int
entry, pos = self.getEntryNLS(slash + path) entry, pos = self.findPrefixOf(path)
dpaLogger.Debugf("getEntryNLS(%s) pos=%v", slash+path, pos) if (pos > 0) && (pos < len(path)) && (path[pos] != '/') {
if pos < len(slash) { return nil, ""
dpaLogger.Debugf("Path '%s' on manifest not found.", path)
return nil, 0
}
if entry != nil {
pos -= len(slash)
dpaLogger.Debugf("Swarm: '%s' matches '%s'.", path, entry.Path)
return
}
slash = slash + "/"
} }
return entry, path[:pos]
} }

View file

@ -28,38 +28,29 @@ func testGetEntry(t *testing.T, path, match string, paths ...string) *manifestTr
} }
func checkEntry(t *testing.T, path, match string, trie *manifestTrie) { func checkEntry(t *testing.T, path, match string, trie *manifestTrie) {
entry, _ := trie.getEntry(path) entry, fullpath := trie.getEntry(path)
if match == "-" && entry != nil { if match == "-" && entry != nil {
t.Errorf("expected no match for '%s', got '%s'", path, entry.Path) t.Errorf("expected no match for '%s', got '%s'", path, fullpath)
} else if entry == nil { } else if entry == nil {
if match != "-" {
t.Errorf("expected entry '%s' to match '%s', got no match", match, path) t.Errorf("expected entry '%s' to match '%s', got no match", match, path)
} else if entry.Path != match { }
t.Errorf("incorrect entry retrieved for '%s'. expected path '%v', got '%s'", path, match, entry.Path) } else if fullpath != match {
t.Errorf("incorrect entry retrieved for '%s'. expected path '%v', got '%s'", path, match, fullpath)
} }
} }
func TestGetEntry(t *testing.T) { func TestGetEntry(t *testing.T) {
// file system manifest always contains regularized paths
testGetEntry(t, "a", "a", "a") testGetEntry(t, "a", "a", "a")
testGetEntry(t, "b", "-", "a") testGetEntry(t, "b", "-", "a")
testGetEntry(t, "/a", "/a", "/a") testGetEntry(t, "/a//", "a", "a")
testGetEntry(t, "/a", "///a", "///a")
testGetEntry(t, "/a", "a", "a")
// fallback // fallback
testGetEntry(t, "/a", "/", "/")
testGetEntry(t, "a", "/", "/")
testGetEntry(t, "/a", "", "") testGetEntry(t, "/a", "", "")
testGetEntry(t, "/a/b", "a/b", "a/b")
// longest/deepest math // longest/deepest math
testGetEntry(t, "a/b", "a/b", "a///", "a/b") testGetEntry(t, "a/b", "a/b", "a", "a/b", "a/bb", "a/b/c")
// trailing slash testGetEntry(t, "//a//b//", "a/b", "a", "a/b", "a/bb", "a/b/c")
testGetEntry(t, "", "", "/", "")
testGetEntry(t, "/", "/", "/", "")
testGetEntry(t, "a", "a", "a", "a/")
testGetEntry(t, "a/", "a/", "a/", "a")
// prefix match
testGetEntry(t, "a", "a", "a", "ab")
testGetEntry(t, "ab", "", "a", "")
testGetEntry(t, "a", "a", "a", "ab")
testGetEntry(t, "a/b", "a", "a", "ab")
} }
func TestDeleteEntry(t *testing.T) { func TestDeleteEntry(t *testing.T) {