-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinfo.go
More file actions
56 lines (50 loc) · 1.04 KB
/
info.go
File metadata and controls
56 lines (50 loc) · 1.04 KB
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
package main
import (
"fmt"
"os"
"os/exec"
"path/filepath"
"strings"
)
func IsLink(p string) bool {
lstat, err := os.Lstat(p)
if err != nil {
// FIXME: have a debug mode, and only print with it
fmt.Println(err.Error())
return false
} else {
return (lstat.Mode() & os.ModeSymlink) != 0
}
}
func ResolvedPath(p string) string {
evaled, _ := filepath.EvalSymlinks(p)
abs, _ := filepath.Abs(evaled)
return abs
}
func IsOpenWrite(p string) bool {
lstat, err := os.Stat(p)
if err != nil {
// FIXME: have a debug mode, and only print with it
fmt.Println(err.Error())
return false
} else {
return (lstat.Mode()>>1)&1 == 1
}
}
func GetMounts() []string {
cmd := exec.Command("findmnt", "-l", "-o", "TARGET", "-n")
bytes, _ := cmd.Output()
text := string(bytes)
return strings.Split(text, "\n")
}
func GetPathRoot(p string, mounts []string) string {
longest := "/"
longestSize := 1
for _, m := range mounts {
if strings.HasPrefix(p, m) && len(m) > longestSize {
longest = m
longestSize = len(m)
}
}
return longest
}