From 3bb68a411550a8cb60161786d94448ac92df7589 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: Mon, 18 Apr 2022 10:16:33 +0000 Subject: [PATCH 1/2] mountinfo: BSDs no longer need cgo MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: 谢致邦 (XIE Zhibang) --- mountinfo/mounted_unix.go | 4 +-- mountinfo/mountinfo_bsd.go | 56 +++++++++++++++++------------- mountinfo/mountinfo_unsupported.go | 4 +-- 3 files changed, 36 insertions(+), 28 deletions(-) diff --git a/mountinfo/mounted_unix.go b/mountinfo/mounted_unix.go index 242f82cc..c7b7678f 100644 --- a/mountinfo/mounted_unix.go +++ b/mountinfo/mounted_unix.go @@ -1,5 +1,5 @@ -//go:build linux || (freebsd && cgo) || (openbsd && cgo) || (darwin && cgo) -// +build linux freebsd,cgo openbsd,cgo darwin,cgo +//go:build linux || freebsd || openbsd || darwin +// +build linux freebsd openbsd darwin package mountinfo diff --git a/mountinfo/mountinfo_bsd.go b/mountinfo/mountinfo_bsd.go index d5513a26..a8d9083c 100644 --- a/mountinfo/mountinfo_bsd.go +++ b/mountinfo/mountinfo_bsd.go @@ -1,43 +1,51 @@ -//go:build (freebsd && cgo) || (openbsd && cgo) || (darwin && cgo) -// +build freebsd,cgo openbsd,cgo darwin,cgo +//go:build freebsd || openbsd || darwin +// +build freebsd openbsd darwin package mountinfo -/* -#include -#include -#include -*/ -import "C" - import ( - "fmt" "reflect" - "unsafe" + "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) { - var rawEntries *C.struct_statfs - - count := int(C.getmntinfo(&rawEntries, C.MNT_WAIT)) - if count == 0 { - return nil, fmt.Errorf("failed to call getmntinfo") + count, err := syscall.Getfsstat(nil, 1 /* MNT_WAIT */) + if err != nil { + return nil, err } - var entries []C.struct_statfs - header := (*reflect.SliceHeader)(unsafe.Pointer(&entries)) - header.Cap = count - header.Len = count - header.Data = uintptr(unsafe.Pointer(rawEntries)) + var 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 - mountinfo.Mountpoint = C.GoString(&entry.f_mntonname[0]) - mountinfo.FSType = C.GoString(&entry.f_fstypename[0]) - mountinfo.Source = C.GoString(&entry.f_mntfromname[0]) + 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 diff --git a/mountinfo/mountinfo_unsupported.go b/mountinfo/mountinfo_unsupported.go index 95769a76..c2e64bc8 100644 --- a/mountinfo/mountinfo_unsupported.go +++ b/mountinfo/mountinfo_unsupported.go @@ -1,5 +1,5 @@ -//go:build (!windows && !linux && !freebsd && !openbsd && !darwin) || (freebsd && !cgo) || (openbsd && !cgo) || (darwin && !cgo) -// +build !windows,!linux,!freebsd,!openbsd,!darwin freebsd,!cgo openbsd,!cgo darwin,!cgo +//go:build !windows && !linux && !freebsd && !openbsd && !darwin +// +build !windows,!linux,!freebsd,!openbsd,!darwin package mountinfo From 4faaf9d38a2e0b2131254628fb63ecd7c7e81ba8 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 00:29:45 +0000 Subject: [PATCH 2/2] Format code with gofumpt. MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: 谢致邦 (XIE Zhibang) --- mountinfo/mountinfo_bsd.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/mountinfo/mountinfo_bsd.go b/mountinfo/mountinfo_bsd.go index a8d9083c..d1ea82ac 100644 --- a/mountinfo/mountinfo_bsd.go +++ b/mountinfo/mountinfo_bsd.go @@ -32,7 +32,7 @@ func parseMountTable(filter FilterFunc) ([]*Info, error) { return nil, err } - var entries = make([]syscall.Statfs_t, count) + entries := make([]syscall.Statfs_t, count) _, err = syscall.Getfsstat(entries, 1 /* MNT_WAIT */) if err != nil { return nil, err