-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpath.go
51 lines (43 loc) · 1.13 KB
/
path.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
package goblin
import (
"fmt"
"strings"
)
func splitPath(path string) ([]string, error) {
tokens := strings.Split(path, pathSeparator)
// Special case the root signifier because it's illegal in any other
// usage and this saves us from doing this check in every iteration
// of the loop below.
if len(tokens) == 1 && tokens[0] == filesystemRootPath {
return tokens, nil
}
err := validatePath(tokens)
if err != nil {
return nil, err
}
return tokens, nil
}
func validatePath(path []string) error {
switch {
case len(path) == 1 && path[0] == filesystemRootPath:
// "." is a special case
return nil
case len(path) == 0:
return fmt.Errorf("path cannot be empty")
case path[0] == pathSeparator:
return fmt.Errorf("path cannot be an absolute path")
}
for _, pathToken := range path {
trimPathToken := strings.TrimSpace(pathToken)
switch {
case trimPathToken == "." || trimPathToken == "..":
return fmt.Errorf("%s is not allowed in paths", trimPathToken)
case trimPathToken == "":
return fmt.Errorf(
"path cannot contain empty segments: %s",
strings.Join(path, pathSeparator),
)
}
}
return nil
}