forked from cookieo9/resources-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
util.go
42 lines (37 loc) · 1.21 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
package resources
import (
"path/filepath"
"github.com/kardianos/osext"
)
// ExecutablePath returns a system-native path to the currently running
// executable.
//
// NOTE: this function was intended to dissapear when it's functionality
// would be handled by the stdlib (go issue 4057). Since that is less
// likely to happen, I will probably leave this here leaving the API
// alone. It is supported via the "github.com/kardianos/osext" package.
func ExecutablePath() (string, error) {
return osext.Executable()
}
// IsNotFound returns true if the error given is an error representing
// a Resource that was not found.
func IsNotFound(e error) bool {
return e == ErrNotFound
}
// CheckPath() returns nil if given a valid path. Valid paths are
// forward slash delimeted, relative paths, which don't escape the
// base-level directory.
//
// Otherwise it returns one of the following error types:
// - ErrEscapeRoot: if the path leaves the base directory
// - ErrNotRelative: if the path is not a relative path
func CheckPath(path string) error {
clean := filepath.Clean(path)
if len(clean) >= 2 && clean[:2] == ".." {
return ErrEscapeRoot
}
if len(clean) >= 1 && clean[0] == '/' {
return ErrNotRelative
}
return nil
}