disk_darwin.go 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. //go:build darwin
  2. // +build darwin
  3. package disk
  4. import (
  5. "context"
  6. "github.com/shirou/gopsutil/v3/internal/common"
  7. "golang.org/x/sys/unix"
  8. )
  9. // PartitionsWithContext returns disk partition.
  10. // 'all' argument is ignored, see: https://github.com/giampaolo/psutil/issues/906
  11. func PartitionsWithContext(ctx context.Context, all bool) ([]PartitionStat, error) {
  12. var ret []PartitionStat
  13. count, err := unix.Getfsstat(nil, unix.MNT_WAIT)
  14. if err != nil {
  15. return ret, err
  16. }
  17. fs := make([]unix.Statfs_t, count)
  18. if _, err = unix.Getfsstat(fs, unix.MNT_WAIT); err != nil {
  19. return ret, err
  20. }
  21. for _, stat := range fs {
  22. opts := []string{"rw"}
  23. if stat.Flags&unix.MNT_RDONLY != 0 {
  24. opts = []string{"ro"}
  25. }
  26. if stat.Flags&unix.MNT_SYNCHRONOUS != 0 {
  27. opts = append(opts, "sync")
  28. }
  29. if stat.Flags&unix.MNT_NOEXEC != 0 {
  30. opts = append(opts, "noexec")
  31. }
  32. if stat.Flags&unix.MNT_NOSUID != 0 {
  33. opts = append(opts, "nosuid")
  34. }
  35. if stat.Flags&unix.MNT_UNION != 0 {
  36. opts = append(opts, "union")
  37. }
  38. if stat.Flags&unix.MNT_ASYNC != 0 {
  39. opts = append(opts, "async")
  40. }
  41. if stat.Flags&unix.MNT_DONTBROWSE != 0 {
  42. opts = append(opts, "nobrowse")
  43. }
  44. if stat.Flags&unix.MNT_AUTOMOUNTED != 0 {
  45. opts = append(opts, "automounted")
  46. }
  47. if stat.Flags&unix.MNT_JOURNALED != 0 {
  48. opts = append(opts, "journaled")
  49. }
  50. if stat.Flags&unix.MNT_MULTILABEL != 0 {
  51. opts = append(opts, "multilabel")
  52. }
  53. if stat.Flags&unix.MNT_NOATIME != 0 {
  54. opts = append(opts, "noatime")
  55. }
  56. if stat.Flags&unix.MNT_NODEV != 0 {
  57. opts = append(opts, "nodev")
  58. }
  59. d := PartitionStat{
  60. Device: common.ByteToString(stat.Mntfromname[:]),
  61. Mountpoint: common.ByteToString(stat.Mntonname[:]),
  62. Fstype: common.ByteToString(stat.Fstypename[:]),
  63. Opts: opts,
  64. }
  65. ret = append(ret, d)
  66. }
  67. return ret, nil
  68. }
  69. func getFsType(stat unix.Statfs_t) string {
  70. return common.ByteToString(stat.Fstypename[:])
  71. }
  72. func SerialNumberWithContext(ctx context.Context, name string) (string, error) {
  73. return "", common.ErrNotImplementedError
  74. }
  75. func LabelWithContext(ctx context.Context, name string) (string, error) {
  76. return "", common.ErrNotImplementedError
  77. }