forked from pachyderm/pachyderm
/
path.go
87 lines (74 loc) · 2.46 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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
// This is a library that provides some path-cleaning and path manipulation
// functions for hashtree.go. The functions it defines are very similar to the
// functions in go's "path" library.
// In both, a canonicalized path has a leading slash and no trailing slash in
// general. The difference is:
//
// - In this library, the canonical version of "/" is "" (i.e. preserve "no
// trailing slash" invariant, at the cost of the "always have a leading slash"
// invariant), whereas
//
// - in go's "path" library, the canonical version of "/" is "/" and "" becomes
// ".".
//
// We prefer our canonicalizion because it gives us globbing behavior we like:
// "/" and "" (canonicalize to: "") match "/" ("") but not "/foo"
// "*" and "/*" (canonicalize to: "/*") match "/foo" but not "/" ("")
package hashtree
import (
fmt "fmt"
"path"
"regexp"
"strings"
)
// internalDefault overrides the internal defaults of many functions in the
// "path" library. Specifically, the top-level dir "/" and the special string
// "." (which is what most "path" functions return for the empty string) both
// map to the empty string here, so that we get the globbing behavior we want
// (see top).
func internalDefault(s string) string {
if s == "/" || s == "." {
return ""
}
return s
}
func externalDefault(s string) string {
if s == "" {
return "/"
}
return s
}
// clean canonicalizes 'path' for internal use: leading slash and no trailing
// slash. Also, clean the result with internalDefault.
func clean(p string) string {
if !strings.HasPrefix(p, "/") {
p = "/" + p
}
return internalDefault(path.Clean(p))
}
// base is like path.Base, but uses this library's defaults for canonical paths
func base(p string) string {
return internalDefault(path.Base(p))
}
// split is like path.Split, but uses this library's defaults for canonical
// paths
func split(p string) (string, string) {
return clean(path.Dir(p)), base(p)
}
// join is like path.Join, but uses our version of 'clean()' instead of
// path.Clean()
func join(ps ...string) string {
return clean(path.Join(ps...))
}
// ValidatePath checks if a file path is legal
func ValidatePath(path string) error {
path = clean(path)
match, _ := regexp.MatchString("^[ -~]+$", path)
if !match {
return fmt.Errorf("path (%v) invalid: only printable ASCII characters allowed", path)
}
if IsGlob(path) {
return fmt.Errorf("path (%v) invalid: globbing character (%v) not allowed in path", path, globRegex.FindString(path))
}
return nil
}