forked from kubescape/node-agent
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpath.go
More file actions
34 lines (27 loc) · 643 Bytes
/
path.go
File metadata and controls
34 lines (27 loc) · 643 Bytes
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
33
34
package utils
import (
"path"
"regexp"
"strings"
)
var headlessProcRegex = regexp.MustCompile(`^/\d+/(task|fd)(/|$)`)
// NormalizePath normalizes a path by:
// 1. Prepending "/proc" to "headless" proc paths (e.g. /46/task/46/fd -> /proc/46/task/46/fd)
// 2. Ensuring it starts with "/" if it's not empty
// 3. Converting "." to "/"
// 4. Cleaning the path (removing redundant slashes, dot-dots, etc.)
func NormalizePath(p string) string {
if p == "" {
return ""
}
if p == "." {
return "/"
}
if headlessProcRegex.MatchString(p) {
p = "/proc" + p
}
if !strings.HasPrefix(p, "/") {
p = "/" + p
}
return path.Clean(p)
}