lib/fs: Improve IsParent (#5658)

This commit is contained in:
Simon Frei
2019-04-22 11:12:32 +02:00
committed by GitHub
parent 926e9228ed
commit e302ccf4b4
2 changed files with 81 additions and 3 deletions

View File

@@ -78,11 +78,25 @@ func WindowsInvalidFilename(name string) bool {
return strings.ContainsAny(name, windowsDisallowedCharacters)
}
// IsParent compares paths purely lexicographically, meaning it returns false
// if path and parent aren't both absolute or relative.
func IsParent(path, parent string) bool {
if len(parent) == 0 {
if parent == path {
// Twice the same root on windows would not be caught at the end.
return false
}
if filepath.IsAbs(path) != filepath.IsAbs(parent) {
return false
}
if parent == "" || parent == "." {
// The empty string is the parent of everything except the empty
// string. (Avoids panic in the next step.)
return len(path) > 0
// string and ".". (Avoids panic in the last step.)
return path != "" && path != "."
}
if parent == "/" {
// The root is the parent of everything except itself, which would
// not be caught below.
return path != "/"
}
if parent[len(parent)-1] != PathSeparator {
parent += string(PathSeparator)