mountinfo_bsd.go 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. //go:build freebsd || openbsd || darwin
  2. // +build freebsd openbsd darwin
  3. package mountinfo
  4. import "golang.org/x/sys/unix"
  5. // parseMountTable returns information about mounted filesystems
  6. func parseMountTable(filter FilterFunc) ([]*Info, error) {
  7. count, err := unix.Getfsstat(nil, unix.MNT_WAIT)
  8. if err != nil {
  9. return nil, err
  10. }
  11. entries := make([]unix.Statfs_t, count)
  12. _, err = unix.Getfsstat(entries, unix.MNT_WAIT)
  13. if err != nil {
  14. return nil, err
  15. }
  16. var out []*Info
  17. for _, entry := range entries {
  18. var skip, stop bool
  19. mountinfo := getMountinfo(&entry)
  20. if filter != nil {
  21. // filter out entries we're not interested in
  22. skip, stop = filter(mountinfo)
  23. if skip {
  24. continue
  25. }
  26. }
  27. out = append(out, mountinfo)
  28. if stop {
  29. break
  30. }
  31. }
  32. return out, nil
  33. }
  34. func mounted(path string) (bool, error) {
  35. path, err := normalizePath(path)
  36. if err != nil {
  37. return false, err
  38. }
  39. // Fast path: compare st.st_dev fields.
  40. // This should always work for FreeBSD and OpenBSD.
  41. mounted, err := mountedByStat(path)
  42. if err == nil {
  43. return mounted, nil
  44. }
  45. // Fallback to parsing mountinfo
  46. return mountedByMountinfo(path)
  47. }