-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlookup-cmds_unix.go
61 lines (56 loc) · 1.08 KB
/
lookup-cmds_unix.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
// +build darwin !android,linux
package wio
import (
"fmt"
"os"
"os/user"
"path/filepath"
"strconv"
"syscall"
)
func lookupExecutables(pattern string) []string {
found := make([]string, 0)
for _, path := range filepath.SplitList(os.Getenv("PATH")) {
matches, err := filepath.Glob(filepath.Join(path, pattern))
if err != nil {
panic(err) // malformed pattern
}
for _, m := range matches {
if isExecutable(m) {
found = append(found, m)
}
}
}
return found
}
func isExecutable(path string) bool {
fi, err := os.Stat(path)
if err != nil {
return false
}
if fi.IsDir() {
return false
}
u, err := user.Current()
var mask uint32 = 0001
st, ok := (fi.Sys()).(*syscall.Stat_t)
if !ok {
panic(fmt.Errorf("Can't get syscall.Stat_t of %s", path))
}
if strconv.FormatUint(uint64(st.Uid), 10) == u.Uid {
mask = 0100
} else {
gid := strconv.FormatUint(uint64(st.Gid), 10)
groups, err := u.GroupIds()
if err != nil {
panic(err)
}
for _, g := range groups {
if g == gid {
mask = 0010
break
}
}
}
return uint32(fi.Mode())&mask != 0
}