"Fossies" - the Fresh Open Source Software Archive

Member "buildah-1.23.1/vendor/github.com/opencontainers/runtime-tools/filepath/ancestor.go" (28 Sep 2021, 714 Bytes) of package /linux/misc/buildah-1.23.1.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) Go source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file.

    1 package filepath
    2 
    3 import (
    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.
   11 func 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 }