2
2
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:
Alexander Neumann 2018-01-05 17:38:52 +01:00
parent 2bc4d200d4
commit aabc0ccaa7
2 changed files with 12 additions and 4 deletions

View File

@ -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
}

View File

@ -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 {