Skip to content

Commit

Permalink
mountinfo: BSDs no longer need reflect
Browse files Browse the repository at this point in the history
Signed-off-by: 谢致邦 (XIE Zhibang) <Yeking@Red54.com>
  • Loading branch information
Red54 committed Apr 19, 2022
1 parent 4faaf9d commit 58b54b6
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 19 deletions.
25 changes: 6 additions & 19 deletions mountinfo/mountinfo_bsd.go
Expand Up @@ -3,24 +3,15 @@

package mountinfo

import (
"reflect"
"syscall"
)
import "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)
}
func getInfo(is []int8) string {
var bs []byte
for i := 0; i < r.Len(); i++ {
i8 := r.Index(i).Int()
if i8 == 0 {
for _, i := range is {
if i == 0 {
break
}
bs = append(bs, byte(i8))
bs = append(bs, byte(i))
}
return string(bs)
}
Expand All @@ -40,12 +31,8 @@ func parseMountTable(filter FilterFunc) ([]*Info, error) {

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 */)
mountinfo := getMountinfo(entry)

if filter != nil {
// filter out entries we're not interested in
Expand Down
13 changes: 13 additions & 0 deletions mountinfo/mountinfo_freebsd.go
@@ -0,0 +1,13 @@
//go:build freebsd || darwin
// +build freebsd darwin

package mountinfo

import "syscall"

func getMountinfo(entry syscall.Statfs_t) (mountinfo Info) {
mountinfo.Mountpoint = getInfo(entry.Mntonname[:])
mountinfo.FSType = getInfo(entry.Fstypename[:])
mountinfo.Source = getInfo(entry.Mntfromname[:])
return
}
13 changes: 13 additions & 0 deletions mountinfo/mountinfo_openbsd.go
@@ -0,0 +1,13 @@
//go:build openbsd
// +build openbsd

package mountinfo

import "syscall"

func getMountinfo(entry syscall.Statfs_t) (mountinfo Info) {
mountinfo.Mountpoint = getInfo(entry.F_mntonname[:])
mountinfo.FSType = getInfo(entry.F_fstypename[:])
mountinfo.Source = getInfo(entry.F_mntfromname[:])
return
}

0 comments on commit 58b54b6

Please sign in to comment.