2020-04-16 05:23:37 -04:00
|
|
|
package mountinfo
|
2017-05-10 17:13:36 -04:00
|
|
|
|
|
|
|
/*
|
|
|
|
#include <sys/param.h>
|
|
|
|
#include <sys/ucred.h>
|
|
|
|
#include <sys/mount.h>
|
|
|
|
*/
|
|
|
|
import "C"
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"reflect"
|
|
|
|
"unsafe"
|
|
|
|
)
|
|
|
|
|
2020-01-06 11:33:05 -05:00
|
|
|
// parseMountTable returns information about mounted filesystems
|
2018-05-17 11:09:10 -04:00
|
|
|
func parseMountTable(filter FilterFunc) ([]*Info, error) {
|
2017-05-10 17:13:36 -04:00
|
|
|
var rawEntries *C.struct_statfs
|
|
|
|
|
|
|
|
count := int(C.getmntinfo(&rawEntries, C.MNT_WAIT))
|
|
|
|
if count == 0 {
|
|
|
|
return nil, fmt.Errorf("Failed to call getmntinfo")
|
|
|
|
}
|
|
|
|
|
|
|
|
var entries []C.struct_statfs
|
|
|
|
header := (*reflect.SliceHeader)(unsafe.Pointer(&entries))
|
|
|
|
header.Cap = count
|
|
|
|
header.Len = count
|
|
|
|
header.Data = uintptr(unsafe.Pointer(rawEntries))
|
|
|
|
|
|
|
|
var out []*Info
|
|
|
|
for _, entry := range entries {
|
|
|
|
var mountinfo Info
|
2018-05-17 11:09:10 -04:00
|
|
|
var skip, stop bool
|
2017-05-10 17:13:36 -04:00
|
|
|
mountinfo.Mountpoint = C.GoString(&entry.f_mntonname[0])
|
2020-04-16 05:23:37 -04:00
|
|
|
mountinfo.Fstype = C.GoString(&entry.f_fstypename[0])
|
|
|
|
mountinfo.Source = C.GoString(&entry.f_mntfromname[0])
|
2018-05-17 11:09:10 -04:00
|
|
|
|
|
|
|
if filter != nil {
|
|
|
|
// filter out entries we're not interested in
|
2019-10-01 21:35:44 -04:00
|
|
|
skip, stop = filter(&mountinfo)
|
2018-05-17 11:09:10 -04:00
|
|
|
if skip {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-05-10 17:13:36 -04:00
|
|
|
out = append(out, &mountinfo)
|
2018-05-17 11:09:10 -04:00
|
|
|
if stop {
|
|
|
|
break
|
|
|
|
}
|
2017-05-10 17:13:36 -04:00
|
|
|
}
|
|
|
|
return out, nil
|
|
|
|
}
|