From 58b54b621e8df327455293b6d23d96c042c5ded2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=B0=A2=E8=87=B4=E9=82=A6=20=28XIE=20Zhibang=29?= Date: Tue, 19 Apr 2022 04:57:46 +0000 Subject: [PATCH] mountinfo: BSDs no longer need reflect MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: 谢致邦 (XIE Zhibang) --- mountinfo/mountinfo_bsd.go | 25 ++++++------------------- mountinfo/mountinfo_freebsd.go | 13 +++++++++++++ mountinfo/mountinfo_openbsd.go | 13 +++++++++++++ 3 files changed, 32 insertions(+), 19 deletions(-) create mode 100644 mountinfo/mountinfo_freebsd.go create mode 100644 mountinfo/mountinfo_openbsd.go diff --git a/mountinfo/mountinfo_bsd.go b/mountinfo/mountinfo_bsd.go index d1ea82ac..5e0db3c3 100644 --- a/mountinfo/mountinfo_bsd.go +++ b/mountinfo/mountinfo_bsd.go @@ -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) } @@ -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 diff --git a/mountinfo/mountinfo_freebsd.go b/mountinfo/mountinfo_freebsd.go new file mode 100644 index 00000000..0f72c27c --- /dev/null +++ b/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 +} diff --git a/mountinfo/mountinfo_openbsd.go b/mountinfo/mountinfo_openbsd.go new file mode 100644 index 00000000..5451b486 --- /dev/null +++ b/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 +}