Skip to content

Commit

Permalink
Merge pull request #1 from Hopetree/develop
Browse files Browse the repository at this point in the history
Develop
  • Loading branch information
Hopetree authored May 12, 2024
2 parents 1c8655d + 21500d8 commit 3d10b27
Showing 1 changed file with 21 additions and 3 deletions.
24 changes: 21 additions & 3 deletions host/disk.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,20 @@
package host

import (
"fmt"
"github.com/shirou/gopsutil/disk"
"runtime"
)

func contains(slice []string, target string) bool {
for _, s := range slice {
if s == target {
return true
}
}
return false
}

func getDiskUsage() (uint64, uint64, error) {
partitions, err := disk.Partitions(false)
if err != nil {
Expand All @@ -26,15 +36,23 @@ func getDiskUsage() (uint64, uint64, error) {
}
}
default:
// 定义一个数组,用来忽略重复的分区,比如群晖
var sli []string
for _, partition := range partitions {
usageStat, err := disk.Usage(partition.Mountpoint)
if err != nil {
continue
}
total += usageStat.Total
used += usageStat.Used
}

s := fmt.Sprintf("%v-%v", usageStat.Total, usageStat.Used)
if contains(sli, s) {
continue
} else {
sli = append(sli, s)
total += usageStat.Total
used += usageStat.Used
}
}
}

DiskTotal := total
Expand Down

0 comments on commit 3d10b27

Please sign in to comment.