2016-05-22 10:46:49 +00:00
|
|
|
// +build freebsd linux darwin
|
|
|
|
|
|
|
|
package disk
|
|
|
|
|
2017-07-28 16:46:44 +00:00
|
|
|
import "golang.org/x/sys/unix"
|
2016-05-22 10:46:49 +00:00
|
|
|
|
2017-07-28 16:46:44 +00:00
|
|
|
// Usage returns a file system usage. path is a filessytem path such
|
|
|
|
// as "/", not device file path like "/dev/vda1". If you want to use
|
|
|
|
// a return value of disk.Partitions, use "Mountpoint" not "Device".
|
2016-05-22 10:46:49 +00:00
|
|
|
func Usage(path string) (*UsageStat, error) {
|
2017-07-28 16:46:44 +00:00
|
|
|
stat := unix.Statfs_t{}
|
|
|
|
err := unix.Statfs(path, &stat)
|
2016-05-22 10:46:49 +00:00
|
|
|
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)),
|
|
|
|
}
|
|
|
|
|
2016-08-30 17:48:07 +00:00
|
|
|
// if could not get InodesTotal, return empty
|
|
|
|
if ret.InodesTotal < ret.InodesFree {
|
|
|
|
return ret, nil
|
|
|
|
}
|
2017-02-02 06:41:29 +00:00
|
|
|
|
2016-05-22 10:46:49 +00:00
|
|
|
ret.InodesUsed = (ret.InodesTotal - ret.InodesFree)
|
|
|
|
ret.Used = (uint64(stat.Blocks) - uint64(stat.Bfree)) * uint64(bsize)
|
2017-02-02 06:41:29 +00:00
|
|
|
|
|
|
|
if ret.InodesTotal == 0 {
|
|
|
|
ret.InodesUsedPercent = 0
|
|
|
|
} else {
|
|
|
|
ret.InodesUsedPercent = (float64(ret.InodesUsed) / float64(ret.InodesTotal)) * 100.0
|
|
|
|
}
|
|
|
|
|
|
|
|
if ret.Total == 0 {
|
|
|
|
ret.UsedPercent = 0
|
|
|
|
} else {
|
|
|
|
ret.UsedPercent = (float64(ret.Used) / float64(ret.Total)) * 100.0
|
|
|
|
}
|
2016-05-22 10:46:49 +00:00
|
|
|
|
|
|
|
return ret, nil
|
|
|
|
}
|