Files
bj_power/bj_power_wms/internal/handler/disk_space_unix.go
T

20 lines
541 B
Go
Raw Normal View History

//go:build !windows
package handler
import "golang.org/x/sys/unix"
// diskUsage 返回指定目录所在磁盘的已用百分比(0-100)、可用字节、总字节。
func diskUsage(path string) (usedPercent int, freeBytes, totalBytes uint64, err error) {
var st unix.Statfs_t
if err := unix.Statfs(path, &st); err != nil {
return 0, 0, 0, err
}
total := st.Blocks * uint64(st.Bsize)
free := st.Bavail * uint64(st.Bsize)
if total == 0 {
return 0, free, total, nil
}
return int((total - free) * 100 / total), free, total, nil
}