35 lines
898 B
Go
35 lines
898 B
Go
// +build freebsd linux darwin
|
|
|
|
package disk
|
|
|
|
import "syscall"
|
|
|
|
func Usage(path string) (*UsageStat, error) {
|
|
stat := syscall.Statfs_t{}
|
|
err := syscall.Statfs(path, &stat)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
bsize := stat.Bsize
|
|
|
|
ret := &UsageStat{
|
|
Path: path,
|
|
Fstype: getFsType(stat),
|
|
Total: (uint64(stat.Blocks) * uint64(bsize)),
|
|
Free: (uint64(stat.Bavail) * uint64(bsize)),
|
|
InodesTotal: (uint64(stat.Files)),
|
|
InodesFree: (uint64(stat.Ffree)),
|
|
}
|
|
|
|
// if could not get InodesTotal, return empty
|
|
if ret.InodesTotal < ret.InodesFree {
|
|
return ret, nil
|
|
}
|
|
ret.InodesUsed = (ret.InodesTotal - ret.InodesFree)
|
|
ret.InodesUsedPercent = (float64(ret.InodesUsed) / float64(ret.InodesTotal)) * 100.0
|
|
ret.Used = (uint64(stat.Blocks) - uint64(stat.Bfree)) * uint64(bsize)
|
|
ret.UsedPercent = (float64(ret.Used) / float64(ret.Total)) * 100.0
|
|
|
|
return ret, nil
|
|
}
|