shirou_gopsutil/disk_unix.go

27 lines
537 B
Go

// +build freebsd linux
package gopsutil
import "syscall"
func DiskUsage(path string) (DiskUsageStat, error) {
stat := syscall.Statfs_t{}
err := syscall.Statfs(path, &stat)
if err != nil {
return DiskUsageStat{Path: path}, err
}
bsize := stat.Bsize / 512
ret := DiskUsageStat{
Path: path,
Total: (uint64(stat.Blocks) * uint64(bsize)) >> 1,
Free: (uint64(stat.Bfree) * uint64(bsize)) >> 1,
}
ret.Used = (ret.Total - ret.Free)
ret.UsedPercent = (float64(ret.Used) / float64(ret.Total)) * 100.0
return ret, nil
}