1package filepath
2
3import (
4	"fmt"
5	"strings"
6)
7
8// IsAncestor returns true when pathB is an strict ancestor of pathA,
9// and false where the paths are equal or pathB is outside of pathA.
10// Paths that are not absolute will be made absolute with Abs.
11func IsAncestor(os, pathA, pathB, cwd string) (_ bool, err error) {
12	if pathA == pathB {
13		return false, nil
14	}
15
16	pathA, err = Abs(os, pathA, cwd)
17	if err != nil {
18		return false, err
19	}
20	pathB, err = Abs(os, pathB, cwd)
21	if err != nil {
22		return false, err
23	}
24	sep := Separator(os)
25	if !strings.HasSuffix(pathA, string(sep)) {
26		pathA = fmt.Sprintf("%s%c", pathA, sep)
27	}
28	if pathA == pathB {
29		return false, nil
30	}
31	return strings.HasPrefix(pathB, pathA), nil
32}
33