-
Notifications
You must be signed in to change notification settings - Fork 568
/
util.go
64 lines (58 loc) · 1.74 KB
/
util.go
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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
package fuse
import (
"path/filepath"
"strings"
)
const Separator = string(filepath.Separator)
// Adds a leading slash and a trailing slash to the path if they don't already exist
func standardizeSlashes(path string) string {
addTrailingSlash := func(p string) string {
if len(p) == 0 {
return Separator
}
if p[len(p)-1] != filepath.Separator {
return p + Separator
}
return p
}
addLeadingSlash := func(p string) string {
if len(p) == 0 {
return Separator
}
if p[0] != filepath.Separator {
return Separator + p
}
return p
}
return addLeadingSlash(addTrailingSlash(path))
}
// Returns true if path1 is at least a grandparent of path2
// along with the intermediate path between path1 and path2
// (excludes last path component of path2)
func isGrandparentOf(path1, path2 string) (bool, string) {
path1 = standardizeSlashes(path1)
path2 = standardizeSlashes(path2)
relative, err := filepath.Rel(path1, path2)
if err != nil {
return false, ""
}
parts := strings.Split(relative, string(filepath.Separator))
if len(parts) < 2 || parts[0] == ".." {
return false, ""
}
intermediate := filepath.Join(parts[:len(parts)-1]...)
return true, standardizeSlashes(intermediate)
}
// Returns true if path1 is parent of path2
func isParentOf(path1, path2 string) bool {
path1 = standardizeSlashes(path1)
path2 = strings.TrimSuffix(standardizeSlashes(path2), Separator) // filepath.Dir doesn't work as expected if path2 ends with a slash
parentDir := filepath.Dir(path2)
return path1 == standardizeSlashes(parentDir)
}
// Returns true if path1 is a descendant of or equal to path2
func isDescendantOf(path1, path2 string) bool {
path1 = standardizeSlashes(path1)
path2 = standardizeSlashes(path2)
return strings.HasPrefix(path1, path2)
}