From 8dfa55df8f41ab538ab59d22977f5726d4111e90 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] mountinfo: BSDs no longer need cgo nor reflect 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 | 44 ++++++++++-------------------- mountinfo/mountinfo_freebsdlike.go | 14 ++++++++++ mountinfo/mountinfo_openbsd.go | 22 +++++++++++++++ mountinfo/mountinfo_unsupported.go | 4 +-- 5 files changed, 54 insertions(+), 34 deletions(-) create mode 100644 mountinfo/mountinfo_freebsdlike.go create mode 100644 mountinfo/mountinfo_openbsd.go 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..8420f58c 100644 --- a/mountinfo/mountinfo_bsd.go +++ b/mountinfo/mountinfo_bsd.go @@ -1,53 +1,37 @@ -//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" -) +import "golang.org/x/sys/unix" // 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 := unix.Getfsstat(nil, unix.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)) + entries := make([]unix.Statfs_t, count) + _, err = unix.Getfsstat(entries, unix.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]) + mountinfo := getMountinfo(&entry) if filter != nil { // filter out entries we're not interested in - skip, stop = filter(&mountinfo) + skip, stop = filter(mountinfo) if skip { continue } } - out = append(out, &mountinfo) + out = append(out, mountinfo) if stop { break } diff --git a/mountinfo/mountinfo_freebsdlike.go b/mountinfo/mountinfo_freebsdlike.go new file mode 100644 index 00000000..952787cd --- /dev/null +++ b/mountinfo/mountinfo_freebsdlike.go @@ -0,0 +1,14 @@ +//go:build freebsd || darwin +// +build freebsd darwin + +package mountinfo + +import "golang.org/x/sys/unix" + +func getMountinfo(entry *unix.Statfs_t) *Info { + var mountinfo Info + mountinfo.Mountpoint = unix.ByteSliceToString(entry.Mntonname[:]) + mountinfo.FSType = unix.ByteSliceToString(entry.Fstypename[:]) + mountinfo.Source = unix.ByteSliceToString(entry.Mntfromname[:]) + return &mountinfo +} diff --git a/mountinfo/mountinfo_openbsd.go b/mountinfo/mountinfo_openbsd.go new file mode 100644 index 00000000..78ea8cf3 --- /dev/null +++ b/mountinfo/mountinfo_openbsd.go @@ -0,0 +1,22 @@ +package mountinfo + +import "golang.org/x/sys/unix" + +func int8SliceToString(is []int8) string { + var bs []byte + for _, i := range is { + if i == 0 { + break + } + bs = append(bs, byte(i)) + } + return string(bs) +} + +func getMountinfo(entry *unix.Statfs_t) *Info { + var mountinfo Info + mountinfo.Mountpoint = int8SliceToString(entry.F_mntonname[:]) + mountinfo.FSType = int8SliceToString(entry.F_fstypename[:]) + mountinfo.Source = int8SliceToString(entry.F_mntfromname[:]) + return &mountinfo +} 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