disk_openbsd.go 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. //go:build openbsd
  2. // +build openbsd
  3. package disk
  4. import (
  5. "bytes"
  6. "context"
  7. "encoding/binary"
  8. "github.com/shirou/gopsutil/v3/internal/common"
  9. "golang.org/x/sys/unix"
  10. )
  11. func PartitionsWithContext(ctx context.Context, all bool) ([]PartitionStat, error) {
  12. var ret []PartitionStat
  13. // get length
  14. count, err := unix.Getfsstat(nil, unix.MNT_WAIT)
  15. if err != nil {
  16. return ret, err
  17. }
  18. fs := make([]unix.Statfs_t, count)
  19. if _, err = unix.Getfsstat(fs, unix.MNT_WAIT); err != nil {
  20. return ret, err
  21. }
  22. for _, stat := range fs {
  23. opts := []string{"rw"}
  24. if stat.F_flags&unix.MNT_RDONLY != 0 {
  25. opts = []string{"rw"}
  26. }
  27. if stat.F_flags&unix.MNT_SYNCHRONOUS != 0 {
  28. opts = append(opts, "sync")
  29. }
  30. if stat.F_flags&unix.MNT_NOEXEC != 0 {
  31. opts = append(opts, "noexec")
  32. }
  33. if stat.F_flags&unix.MNT_NOSUID != 0 {
  34. opts = append(opts, "nosuid")
  35. }
  36. if stat.F_flags&unix.MNT_NODEV != 0 {
  37. opts = append(opts, "nodev")
  38. }
  39. if stat.F_flags&unix.MNT_ASYNC != 0 {
  40. opts = append(opts, "async")
  41. }
  42. if stat.F_flags&unix.MNT_SOFTDEP != 0 {
  43. opts = append(opts, "softdep")
  44. }
  45. if stat.F_flags&unix.MNT_NOATIME != 0 {
  46. opts = append(opts, "noatime")
  47. }
  48. if stat.F_flags&unix.MNT_WXALLOWED != 0 {
  49. opts = append(opts, "wxallowed")
  50. }
  51. d := PartitionStat{
  52. Device: common.ByteToString(stat.F_mntfromname[:]),
  53. Mountpoint: common.ByteToString(stat.F_mntonname[:]),
  54. Fstype: common.ByteToString(stat.F_fstypename[:]),
  55. Opts: opts,
  56. }
  57. ret = append(ret, d)
  58. }
  59. return ret, nil
  60. }
  61. func IOCountersWithContext(ctx context.Context, names ...string) (map[string]IOCountersStat, error) {
  62. ret := make(map[string]IOCountersStat)
  63. r, err := unix.SysctlRaw("hw.diskstats")
  64. if err != nil {
  65. return nil, err
  66. }
  67. buf := []byte(r)
  68. length := len(buf)
  69. count := int(uint64(length) / uint64(sizeOfDiskstats))
  70. // parse buf to Diskstats
  71. for i := 0; i < count; i++ {
  72. b := buf[i*sizeOfDiskstats : i*sizeOfDiskstats+sizeOfDiskstats]
  73. d, err := parseDiskstats(b)
  74. if err != nil {
  75. continue
  76. }
  77. name := common.IntToString(d.Name[:])
  78. if len(names) > 0 && !common.StringsHas(names, name) {
  79. continue
  80. }
  81. ds := IOCountersStat{
  82. ReadCount: d.Rxfer,
  83. WriteCount: d.Wxfer,
  84. ReadBytes: d.Rbytes,
  85. WriteBytes: d.Wbytes,
  86. Name: name,
  87. }
  88. ret[name] = ds
  89. }
  90. return ret, nil
  91. }
  92. // BT2LD(time) ((long double)(time).sec + (time).frac * BINTIME_SCALE)
  93. func parseDiskstats(buf []byte) (Diskstats, error) {
  94. var ds Diskstats
  95. br := bytes.NewReader(buf)
  96. // err := binary.Read(br, binary.LittleEndian, &ds)
  97. err := common.Read(br, binary.LittleEndian, &ds)
  98. if err != nil {
  99. return ds, err
  100. }
  101. return ds, nil
  102. }
  103. func UsageWithContext(ctx context.Context, path string) (*UsageStat, error) {
  104. stat := unix.Statfs_t{}
  105. err := unix.Statfs(path, &stat)
  106. if err != nil {
  107. return nil, err
  108. }
  109. bsize := stat.F_bsize
  110. ret := &UsageStat{
  111. Path: path,
  112. Fstype: getFsType(stat),
  113. Total: (uint64(stat.F_blocks) * uint64(bsize)),
  114. Free: (uint64(stat.F_bavail) * uint64(bsize)),
  115. InodesTotal: (uint64(stat.F_files)),
  116. InodesFree: (uint64(stat.F_ffree)),
  117. }
  118. ret.InodesUsed = (ret.InodesTotal - ret.InodesFree)
  119. ret.InodesUsedPercent = (float64(ret.InodesUsed) / float64(ret.InodesTotal)) * 100.0
  120. ret.Used = (uint64(stat.F_blocks) - uint64(stat.F_bfree)) * uint64(bsize)
  121. ret.UsedPercent = (float64(ret.Used) / float64(ret.Total)) * 100.0
  122. return ret, nil
  123. }
  124. func getFsType(stat unix.Statfs_t) string {
  125. return common.ByteToString(stat.F_fstypename[:])
  126. }
  127. func SerialNumberWithContext(ctx context.Context, name string) (string, error) {
  128. return "", common.ErrNotImplementedError
  129. }
  130. func LabelWithContext(ctx context.Context, name string) (string, error) {
  131. return "", common.ErrNotImplementedError
  132. }