2
2
mirror of https://github.com/octoleo/restic.git synced 2024-11-01 03:12:31 +00:00
restic/internal/fs/path_prefix.go

43 lines
733 B
Go
Raw Normal View History

2017-09-11 19:29:37 +00:00
package fs
import (
"path/filepath"
)
// HasPathPrefix returns true if p is a subdir of (or a file within) base. It
// 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.
2017-09-11 19:29:37 +00:00
func HasPathPrefix(base, p string) bool {
if filepath.VolumeName(base) != filepath.VolumeName(p) {
return false
}
// handle case when base and p are not of the same type
if filepath.IsAbs(base) != filepath.IsAbs(p) {
2017-09-11 19:29:37 +00:00
return false
}
base = filepath.Clean(base)
p = filepath.Clean(p)
if base == p {
return true
}
for {
dir := filepath.Dir(p)
if base == dir {
return true
}
if p == dir {
break
}
p = dir
}
return false
}