mirror of
https://github.com/distribution/distribution.git
synced 2026-10-28 10:15:27 +08:00
Fix inmemory driver find traversing too deep into directory nodes (#4996)
This commit is contained in:
2 files changed
+47
-1
No files matched your search
@@ -77,7 +77,7 @@ func (d *dir) find(q string) node {
|
||||
return d
|
||||
}
|
||||
|
||||
if child.isdir() {
|
||||
if child.isdir() && i >= 0 {
|
||||
// traverse down!
|
||||
q = q[i+1:]
|
||||
return child.(*dir).find(q)
|
||||
|
||||
@@ -0,0 +1,46 @@
|
||||
package inmemory
|
||||
|
||||
import (
|
||||
"testing"
|
||||
"time"
|
||||
)
|
||||
|
||||
// TestFindSameNamedChild checks that find returns a directory itself, not its
|
||||
// same-named child.
|
||||
func TestFindSameNamedChild(t *testing.T) {
|
||||
root := &dir{common: common{p: "/", mod: time.Now()}}
|
||||
|
||||
if _, err := root.mkdirs("/dirname/dirname"); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if _, err := root.mkfile("/dirname/file"); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
for _, tc := range []struct {
|
||||
query string
|
||||
path string
|
||||
isdir bool
|
||||
}{
|
||||
{"/dirname", "/dirname", true},
|
||||
{"dirname", "/dirname", true},
|
||||
{"/dirname/", "/dirname", true},
|
||||
{"/dirname/dirname", "/dirname/dirname", true},
|
||||
{"/dirname/file", "/dirname/file", false},
|
||||
|
||||
// not found, so the closest existing parent is returned
|
||||
{"/dirname/dirname/dirname", "/dirname/dirname", true},
|
||||
{"/dirname/missing", "/dirname", true},
|
||||
{"/missing", "/", true},
|
||||
} {
|
||||
t.Run(tc.query, func(t *testing.T) {
|
||||
n := root.find(tc.query)
|
||||
if n.path() != tc.path {
|
||||
t.Errorf("find(%q).path() = %q, want %q", tc.query, n.path(), tc.path)
|
||||
}
|
||||
if n.isdir() != tc.isdir {
|
||||
t.Errorf("find(%q).isdir() = %v, want %v", tc.query, n.isdir(), tc.isdir)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user