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..75d3e96a 100644 --- a/mountinfo/mountinfo_bsd.go +++ b/mountinfo/mountinfo_bsd.go @@ -1,43 +1,39 @@ -//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 "syscall" -import ( - "fmt" - "reflect" - "unsafe" -) +func int8SliceToString(is []int8) string { + var bs []byte + for _, i := range is { + if i == 0 { + break + } + bs = append(bs, byte(i)) + } + 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") + const MNT_WAIT = 1 + count, err := syscall.Getfsstat(nil, 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([]syscall.Statfs_t, count) + _, err = syscall.Getfsstat(entries, 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 diff --git a/mountinfo/mountinfo_freebsdlike.go b/mountinfo/mountinfo_freebsdlike.go new file mode 100644 index 00000000..24b4f698 --- /dev/null +++ b/mountinfo/mountinfo_freebsdlike.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 = int8SliceToString(entry.Mntonname[:]) + mountinfo.FSType = int8SliceToString(entry.Fstypename[:]) + mountinfo.Source = int8SliceToString(entry.Mntfromname[:]) + return +} diff --git a/mountinfo/mountinfo_openbsd.go b/mountinfo/mountinfo_openbsd.go new file mode 100644 index 00000000..f1b5d9d2 --- /dev/null +++ b/mountinfo/mountinfo_openbsd.go @@ -0,0 +1,10 @@ +package mountinfo + +import "syscall" + +func getMountinfo(entry syscall.Statfs_t) (mountinfo Info) { + mountinfo.Mountpoint = int8SliceToString(entry.F_mntonname[:]) + mountinfo.FSType = int8SliceToString(entry.F_fstypename[:]) + mountinfo.Source = int8SliceToString(entry.F_mntfromname[:]) + return +} 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