mirror of
https://github.com/octoleo/restic.git
synced 2024-11-25 06:07:44 +00:00
fs: Make HasPathPrefix work with relative paths
This commit is contained in:
parent
2bc4d200d4
commit
aabc0ccaa7
@ -5,14 +5,15 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
// HasPathPrefix returns true if p is a subdir of (or a file within) base. It
|
// HasPathPrefix returns true if p is a subdir of (or a file within) base. It
|
||||||
// assumes a file system which is case sensitive. For relative paths, false is
|
// assumes a file system which is case sensitive. If the paths are not of the
|
||||||
// returned.
|
// same type (one is relative, the other is absolute), false is returned.
|
||||||
func HasPathPrefix(base, p string) bool {
|
func HasPathPrefix(base, p string) bool {
|
||||||
if filepath.VolumeName(base) != filepath.VolumeName(p) {
|
if filepath.VolumeName(base) != filepath.VolumeName(p) {
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
|
||||||
if !filepath.IsAbs(base) || !filepath.IsAbs(p) {
|
// handle case when base and p are not of the same type
|
||||||
|
if filepath.IsAbs(base) != filepath.IsAbs(p) {
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -21,7 +21,10 @@ func TestHasPathPrefix(t *testing.T) {
|
|||||||
base, p string
|
base, p string
|
||||||
result bool
|
result bool
|
||||||
}{
|
}{
|
||||||
{"", "", false},
|
{"", "", true},
|
||||||
|
{".", ".", true},
|
||||||
|
{".", "foo", true},
|
||||||
|
{"foo", ".", false},
|
||||||
{"/", "", false},
|
{"/", "", false},
|
||||||
{"/", "x", false},
|
{"/", "x", false},
|
||||||
{"x", "/", false},
|
{"x", "/", false},
|
||||||
@ -36,6 +39,10 @@ func TestHasPathPrefix(t *testing.T) {
|
|||||||
{"/home/user/foo", "/home/user/foobar", false},
|
{"/home/user/foo", "/home/user/foobar", false},
|
||||||
{"/home/user/Foo", "/home/user/foo/bar/baz", false},
|
{"/home/user/Foo", "/home/user/foo/bar/baz", false},
|
||||||
{"/home/user/foo", "/home/user/Foo/bar/baz", false},
|
{"/home/user/foo", "/home/user/Foo/bar/baz", false},
|
||||||
|
{"user/foo", "user/foo/bar/baz", true},
|
||||||
|
{"user/foo", "./user/foo", true},
|
||||||
|
{"user/foo", "./user/foo/", true},
|
||||||
|
{"/home/user/foo", "./user/foo/", false},
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, test := range tests {
|
for _, test := range tests {
|
||||||
|
Loading…
Reference in New Issue
Block a user