2023-02-01 17:06:36 +00:00
|
|
|
//go:build freebsd || openbsd || darwin
|
|
|
|
// +build freebsd openbsd darwin
|
2022-07-15 09:53:35 +00:00
|
|
|
|
|
|
|
package mountinfo
|
|
|
|
|
2023-02-01 17:06:36 +00:00
|
|
|
import "golang.org/x/sys/unix"
|
2022-07-15 09:53:35 +00:00
|
|
|
|
|
|
|
// parseMountTable returns information about mounted filesystems
|
|
|
|
func parseMountTable(filter FilterFunc) ([]*Info, error) {
|
2023-02-01 17:06:36 +00:00
|
|
|
count, err := unix.Getfsstat(nil, unix.MNT_WAIT)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2022-07-15 09:53:35 +00:00
|
|
|
}
|
|
|
|
|
2023-02-01 17:06:36 +00:00
|
|
|
entries := make([]unix.Statfs_t, count)
|
|
|
|
_, err = unix.Getfsstat(entries, unix.MNT_WAIT)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2022-07-15 09:53:35 +00:00
|
|
|
|
|
|
|
var out []*Info
|
|
|
|
for _, entry := range entries {
|
|
|
|
var skip, stop bool
|
2023-02-01 17:06:36 +00:00
|
|
|
mountinfo := getMountinfo(&entry)
|
2022-07-15 09:53:35 +00:00
|
|
|
|
|
|
|
if filter != nil {
|
|
|
|
// filter out entries we're not interested in
|
2023-02-01 17:06:36 +00:00
|
|
|
skip, stop = filter(mountinfo)
|
2022-07-15 09:53:35 +00:00
|
|
|
if skip {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-02-01 17:06:36 +00:00
|
|
|
out = append(out, mountinfo)
|
2022-07-15 09:53:35 +00:00
|
|
|
if stop {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return out, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func mounted(path string) (bool, error) {
|
|
|
|
path, err := normalizePath(path)
|
|
|
|
if err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
// Fast path: compare st.st_dev fields.
|
|
|
|
// This should always work for FreeBSD and OpenBSD.
|
|
|
|
mounted, err := mountedByStat(path)
|
|
|
|
if err == nil {
|
|
|
|
return mounted, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Fallback to parsing mountinfo
|
|
|
|
return mountedByMountinfo(path)
|
|
|
|
}
|