-
Notifications
You must be signed in to change notification settings - Fork 0
/
mountinfo_bsd.go
80 lines (70 loc) · 1.69 KB
/
mountinfo_bsd.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
//go:build freebsd || openbsd || darwin
// +build freebsd openbsd darwin
package mountinfo
import (
"reflect"
"syscall"
)
func getInfo(r reflect.Value, a string, b string) string {
if r.FieldByName(a) != (reflect.Value{}) {
r = r.FieldByName(a)
} else {
r = r.FieldByName(b)
}
var bs []byte
for i := 0; i < r.Len(); i++ {
i8 := r.Index(i).Int()
if i8 == 0 {
break
}
bs = append(bs, byte(i8))
}
return string(bs)
}
// parseMountTable returns information about mounted filesystems
func parseMountTable(filter FilterFunc) ([]*Info, error) {
count, err := syscall.Getfsstat(nil, 1 /* MNT_WAIT */)
if err != nil {
return nil, err
}
entries := make([]syscall.Statfs_t, count)
_, err = syscall.Getfsstat(entries, 1 /* MNT_WAIT */)
if err != nil {
return nil, err
}
var out []*Info
for _, entry := range entries {
var mountinfo Info
var skip, stop bool
r := reflect.ValueOf(entry)
mountinfo.Mountpoint = getInfo(r, "Mntonname", "F_mntonname" /* OpenBSD */)
mountinfo.FSType = getInfo(r, "Fstypename", "F_fstypename" /* OpenBSD */)
mountinfo.Source = getInfo(r, "Mntfromname", "F_mntfromname" /* OpenBSD */)
if filter != nil {
// filter out entries we're not interested in
skip, stop = filter(&mountinfo)
if skip {
continue
}
}
out = append(out, &mountinfo)
if stop {
break
}
}
return out, nil
}
func mounted(path string) (bool, error) {
path, err := normalizePath(path)
if err != nil {
return false, err
}
// Fast path: compare st.st_dev fields.
// This should always work for FreeBSD and OpenBSD.
mounted, err := mountedByStat(path)
if err == nil {
return mounted, nil
}
// Fallback to parsing mountinfo
return mountedByMountinfo(path)
}