forked from TrueCloudLab/restic
fs: Make HasPathPrefix work with relative paths
This commit is contained in:
parent
2bc4d200d4
commit
aabc0ccaa7
2 changed files with 12 additions and 4 deletions
|
@ -5,14 +5,15 @@ import (
|
|||
)
|
||||
|
||||
// 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
|
||||
// returned.
|
||||
// assumes a file system which is case sensitive. If the paths are not of the
|
||||
// same type (one is relative, the other is absolute), false is returned.
|
||||
func HasPathPrefix(base, p string) bool {
|
||||
if filepath.VolumeName(base) != filepath.VolumeName(p) {
|
||||
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
|
||||
}
|
||||
|
||||
|
|
|
@ -21,7 +21,10 @@ func TestHasPathPrefix(t *testing.T) {
|
|||
base, p string
|
||||
result bool
|
||||
}{
|
||||
{"", "", false},
|
||||
{"", "", true},
|
||||
{".", ".", true},
|
||||
{".", "foo", true},
|
||||
{"foo", ".", false},
|
||||
{"/", "", 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/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 {
|
||||
|
|
Loading…
Reference in a new issue