Skip to content

Commit 5534f4a

Browse files
author
deiu
committed
Improvied the walkPath function
1 parent 4c8f6ec commit 5534f4a

File tree

2 files changed

+16
-10
lines changed

2 files changed

+16
-10
lines changed

Diff for: acl.go

+13-8
Original file line numberDiff line numberDiff line change
@@ -36,9 +36,9 @@ func (acl *WAC) allow(mode string, path string) (int, error) {
3636
if err != nil {
3737
return 500, err
3838
}
39-
depth := strings.Split("/"+p.Path, "/")
39+
depth := strings.Split(p.Path, "/")
4040

41-
for d := len(depth) - 1; d >= 0; d-- {
41+
for d := len(depth); d >= 0; d-- {
4242
p, err := acl.srv.pathInfo(path)
4343
if err != nil {
4444
return 500, err
@@ -151,17 +151,22 @@ func (acl *WAC) allow(mode string, path string) (int, error) {
151151
accessType = "defaultForNew"
152152

153153
// cd one level: walkPath("/foo/bar/baz") => /foo/bar/
154-
path = walkPath(p.Base, depth, d)
154+
// decrement depth
155+
if len(depth) > 0 {
156+
depth = depth[:len(depth)-1]
157+
} else {
158+
depth = depth[:1]
159+
}
160+
path = walkPath(p.Base, depth)
155161
}
156162
acl.srv.debug.Println("No ACL policies present - access allowed")
157163
return 200, nil
158164
}
159165

160-
func walkPath(base string, depth []string, d int) string {
161-
depth = depth[:d]
162-
path := base + "/" + strings.Join(depth, "/")
163-
if d > 0 {
164-
path += "/"
166+
func walkPath(base string, depth []string) string {
167+
path := base + "/"
168+
if len(depth) > 0 {
169+
path += strings.Join(depth, "/") + "/"
165170
}
166171
return path
167172
}

Diff for: acl_test.go

+3-2
Original file line numberDiff line numberDiff line change
@@ -948,8 +948,9 @@ func TestACLwalkPath(t *testing.T) {
948948
depth := strings.Split(p.Path, "/")
949949
var results []string
950950

951-
for i := len(depth) - 1; i >= 0; i-- {
952-
path = walkPath(p.Base, depth, i)
951+
for i := len(depth); i > 0; i-- {
952+
depth = depth[:len(depth)-1]
953+
path = walkPath(p.Base, depth)
953954
results = append(results, path)
954955
}
955956
assert.Equal(t, "http://example.org/foo/bar/", results[0])

0 commit comments

Comments
 (0)