lib: Introduce fs.IsParent (fixes #5324) (#5326)

This commit is contained in:
Simon Frei
2018-11-22 11:16:45 +01:00
committed by Jakob Borg
parent 513d3bc374
commit 2f9840ddae
6 changed files with 19 additions and 11 deletions

View File

@@ -77,3 +77,15 @@ func WindowsInvalidFilename(name string) bool {
// The path must not contain any disallowed characters
return strings.ContainsAny(name, windowsDisallowedCharacters)
}
func IsParent(path, parent string) bool {
if len(parent) == 0 {
// The empty string is the parent of everything except the empty
// string. (Avoids panic in the next step.)
return len(path) > 0
}
if parent[len(parent)-1] != PathSeparator {
parent += string(PathSeparator)
}
return strings.HasPrefix(path, parent)
}