aboutsummaryrefslogtreecommitdiff
path: root/vendor/github.com/opencontainers/runtime-tools/filepath
diff options
context:
space:
mode:
Diffstat (limited to 'vendor/github.com/opencontainers/runtime-tools/filepath')
-rw-r--r--vendor/github.com/opencontainers/runtime-tools/filepath/abs.go52
-rw-r--r--vendor/github.com/opencontainers/runtime-tools/filepath/ancestor.go32
-rw-r--r--vendor/github.com/opencontainers/runtime-tools/filepath/clean.go56
-rw-r--r--vendor/github.com/opencontainers/runtime-tools/filepath/doc.go6
-rw-r--r--vendor/github.com/opencontainers/runtime-tools/filepath/join.go9
-rw-r--r--vendor/github.com/opencontainers/runtime-tools/filepath/separator.go9
6 files changed, 164 insertions, 0 deletions
diff --git a/vendor/github.com/opencontainers/runtime-tools/filepath/abs.go b/vendor/github.com/opencontainers/runtime-tools/filepath/abs.go
new file mode 100644
index 000000000..c19bba26a
--- /dev/null
+++ b/vendor/github.com/opencontainers/runtime-tools/filepath/abs.go
@@ -0,0 +1,52 @@
+package filepath
+
+import (
+ "errors"
+ "regexp"
+ "strings"
+)
+
+var windowsAbs = regexp.MustCompile(`^[a-zA-Z]:\\.*$`)
+
+// Abs is a version of path/filepath's Abs with an explicit operating
+// system and current working directory.
+func Abs(os, path, cwd string) (_ string, err error) {
+ if os == "windows" {
+ return "", errors.New("Abs() does not support windows yet")
+ }
+ if IsAbs(os, path) {
+ return Clean(os, path), nil
+ }
+ return Clean(os, Join(os, cwd, path)), nil
+}
+
+// IsAbs is a version of path/filepath's IsAbs with an explicit
+// operating system.
+func IsAbs(os, path string) bool {
+ if os == "windows" {
+ // FIXME: copy hideous logic from Go's
+ // src/path/filepath/path_windows.go into somewhere where we can
+ // put 3-clause BSD licensed code.
+ return windowsAbs.MatchString(path)
+ }
+ sep := Separator(os)
+
+ // POSIX has [1]:
+ //
+ // > If a pathname begins with two successive <slash> characters,
+ // > the first component following the leading <slash> characters
+ // > may be interpreted in an implementation-defined manner,
+ // > although more than two leading <slash> characters shall be
+ // > treated as a single <slash> character.
+ //
+ // And Boost treats // as non-absolute [2], but Linux [3,4], Python
+ // [5] and Go [6] all treat // as absolute.
+ //
+ // [1]: http://pubs.opengroup.org/onlinepubs/9699919799/basedefs/V1_chap04.html#tag_04_13
+ // [2]: https://github.com/boostorg/filesystem/blob/boost-1.64.0/test/path_test.cpp#L861
+ // [3]: http://man7.org/linux/man-pages/man7/path_resolution.7.html
+ // [4]: https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/Documentation/filesystems/path-lookup.md?h=v4.12#n41
+ // [5]: https://github.com/python/cpython/blob/v3.6.1/Lib/posixpath.py#L64-L66
+ // [6]: https://go.googlesource.com/go/+/go1.8.3/src/path/path.go#199
+ return strings.HasPrefix(path, string(sep))
+}
diff --git a/vendor/github.com/opencontainers/runtime-tools/filepath/ancestor.go b/vendor/github.com/opencontainers/runtime-tools/filepath/ancestor.go
new file mode 100644
index 000000000..896cd8206
--- /dev/null
+++ b/vendor/github.com/opencontainers/runtime-tools/filepath/ancestor.go
@@ -0,0 +1,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
+}
diff --git a/vendor/github.com/opencontainers/runtime-tools/filepath/clean.go b/vendor/github.com/opencontainers/runtime-tools/filepath/clean.go
new file mode 100644
index 000000000..b70c575f2
--- /dev/null
+++ b/vendor/github.com/opencontainers/runtime-tools/filepath/clean.go
@@ -0,0 +1,56 @@
+package filepath
+
+import (
+ "fmt"
+ "strings"
+)
+
+// Clean is an explicit-OS version of path/filepath's Clean.
+func Clean(os, path string) string {
+ abs := IsAbs(os, path)
+ sep := Separator(os)
+ elements := strings.Split(path, string(sep))
+
+ // Replace multiple Separator elements with a single one.
+ for i := 0; i < len(elements); i++ {
+ if len(elements[i]) == 0 {
+ elements = append(elements[:i], elements[i+1:]...)
+ i--
+ }
+ }
+
+ // Eliminate each . path name element (the current directory).
+ for i := 0; i < len(elements); i++ {
+ if elements[i] == "." && len(elements) > 1 {
+ elements = append(elements[:i], elements[i+1:]...)
+ i--
+ }
+ }
+
+ // Eliminate each inner .. path name element (the parent directory)
+ // along with the non-.. element that precedes it.
+ for i := 1; i < len(elements); i++ {
+ if i > 0 && elements[i] == ".." {
+ elements = append(elements[:i-1], elements[i+1:]...)
+ i -= 2
+ }
+ }
+
+ // Eliminate .. elements that begin a rooted path:
+ // that is, replace "/.." by "/" at the beginning of a path,
+ // assuming Separator is '/'.
+ if abs && len(elements) > 0 {
+ for elements[0] == ".." {
+ elements = elements[1:]
+ }
+ }
+
+ cleaned := strings.Join(elements, string(sep))
+ if abs {
+ cleaned = fmt.Sprintf("%c%s", sep, cleaned)
+ }
+ if cleaned == path {
+ return path
+ }
+ return Clean(os, cleaned)
+}
diff --git a/vendor/github.com/opencontainers/runtime-tools/filepath/doc.go b/vendor/github.com/opencontainers/runtime-tools/filepath/doc.go
new file mode 100644
index 000000000..7ee085bf4
--- /dev/null
+++ b/vendor/github.com/opencontainers/runtime-tools/filepath/doc.go
@@ -0,0 +1,6 @@
+// Package filepath implements Go's filepath package with explicit
+// operating systems (and for some functions and explicit working
+// directory). This allows tools built for one OS to operate on paths
+// targeting another OS. For example, a Linux build can determine
+// whether a path is absolute on Linux or on Windows.
+package filepath
diff --git a/vendor/github.com/opencontainers/runtime-tools/filepath/join.go b/vendor/github.com/opencontainers/runtime-tools/filepath/join.go
new file mode 100644
index 000000000..b865d237c
--- /dev/null
+++ b/vendor/github.com/opencontainers/runtime-tools/filepath/join.go
@@ -0,0 +1,9 @@
+package filepath
+
+import "strings"
+
+// Join is an explicit-OS version of path/filepath's Join.
+func Join(os string, elem ...string) string {
+ sep := Separator(os)
+ return Clean(os, strings.Join(elem, string(sep)))
+}
diff --git a/vendor/github.com/opencontainers/runtime-tools/filepath/separator.go b/vendor/github.com/opencontainers/runtime-tools/filepath/separator.go
new file mode 100644
index 000000000..2c5e8905a
--- /dev/null
+++ b/vendor/github.com/opencontainers/runtime-tools/filepath/separator.go
@@ -0,0 +1,9 @@
+package filepath
+
+// Separator is an explicit-OS version of path/filepath's Separator.
+func Separator(os string) rune {
+ if os == "windows" {
+ return '\\'
+ }
+ return '/'
+}