Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

mountinfo: BSDs no longer need cgo #113

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions mountinfo/mounted_unix.go
Original file line number Diff line number Diff line change
@@ -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

Expand Down
56 changes: 32 additions & 24 deletions mountinfo/mountinfo_bsd.go
Original file line number Diff line number Diff line change
@@ -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 <sys/param.h>
#include <sys/ucred.h>
#include <sys/mount.h>
*/
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)
}
Copy link
Member

@AkihiroSuda AkihiroSuda Apr 18, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Rather than depending on reflects, can we split mountinfo_bsd.go into mountinfo_{freebsd,openbsd,darwin}.go?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Okay, I will create a new PR without reflect.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done by #114


// 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))
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
Expand Down
4 changes: 2 additions & 2 deletions mountinfo/mountinfo_unsupported.go
Original file line number Diff line number Diff line change
@@ -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

Expand Down