File: ancestor.go

package info (click to toggle)
golang-github-opencontainers-runtime-tools 0.8.0%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 1,112 kB
  • sloc: sh: 544; makefile: 102
file content (32 lines) | stat: -rw-r--r-- 714 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
package filepath

import (
	"fmt"
	"strings"
)

// IsAncestor returns true when pathB is an strict ancestor of pathA,
// and false where the paths are equal or pathB is outside of pathA.
// Paths that are not absolute will be made absolute with Abs.
func IsAncestor(os, pathA, pathB, cwd string) (_ bool, err error) {
	if pathA == pathB {
		return false, nil
	}

	pathA, err = Abs(os, pathA, cwd)
	if err != nil {
		return false, err
	}
	pathB, err = Abs(os, pathB, cwd)
	if err != nil {
		return false, err
	}
	sep := Separator(os)
	if !strings.HasSuffix(pathA, string(sep)) {
		pathA = fmt.Sprintf("%s%c", pathA, sep)
	}
	if pathA == pathB {
		return false, nil
	}
	return strings.HasPrefix(pathB, pathA), nil
}