2014-08-08 22:09:28 +08:00
|
|
|
// +build freebsd linux darwin
|
2014-04-18 15:34:47 +08:00
|
|
|
|
2014-12-30 21:09:05 +08:00
|
|
|
package disk
|
2014-04-18 15:34:47 +08:00
|
|
|
|
2017-12-31 14:25:49 +08:00
|
|
|
import (
|
|
|
|
"context"
|
2018-07-24 06:24:05 +08:00
|
|
|
"strconv"
|
2017-12-31 14:25:49 +08:00
|
|
|
|
|
|
|
"golang.org/x/sys/unix"
|
|
|
|
)
|
2014-04-18 15:34:47 +08:00
|
|
|
|
2017-07-14 14:33:53 +08: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-03-22 22:09:12 +08:00
|
|
|
func Usage(path string) (*UsageStat, error) {
|
2017-12-31 14:25:49 +08:00
|
|
|
return UsageWithContext(context.Background(), path)
|
|
|
|
}
|
|
|
|
|
|
|
|
func UsageWithContext(ctx context.Context, path string) (*UsageStat, error) {
|
2017-06-03 04:51:00 +08:00
|
|
|
stat := unix.Statfs_t{}
|
|
|
|
err := unix.Statfs(path, &stat)
|
2014-04-18 15:34:47 +08:00
|
|
|
if err != nil {
|
2014-05-01 14:47:46 +08:00
|
|
|
return nil, err
|
2014-04-18 15:34:47 +08:00
|
|
|
}
|
2014-10-10 16:09:36 +08:00
|
|
|
bsize := stat.Bsize
|
2014-04-18 15:34:47 +08:00
|
|
|
|
2016-03-22 22:09:12 +08:00
|
|
|
ret := &UsageStat{
|
2018-07-24 06:24:05 +08:00
|
|
|
Path: unescapeFstab(path),
|
2015-08-14 17:08:43 +08:00
|
|
|
Fstype: getFsType(stat),
|
2014-10-10 16:09:36 +08:00
|
|
|
Total: (uint64(stat.Blocks) * uint64(bsize)),
|
2015-12-01 22:29:15 +08:00
|
|
|
Free: (uint64(stat.Bavail) * uint64(bsize)),
|
2014-08-26 16:38:52 +08:00
|
|
|
InodesTotal: (uint64(stat.Files)),
|
2014-08-26 21:17:35 +08:00
|
|
|
InodesFree: (uint64(stat.Ffree)),
|
2014-04-18 15:34:47 +08:00
|
|
|
}
|
|
|
|
|
2016-08-27 13:02:51 +08:00
|
|
|
// if could not get InodesTotal, return empty
|
|
|
|
if ret.InodesTotal < ret.InodesFree {
|
|
|
|
return ret, nil
|
2017-02-02 07:05:29 +08:00
|
|
|
}
|
|
|
|
|
2014-08-26 16:38:52 +08:00
|
|
|
ret.InodesUsed = (ret.InodesTotal - ret.InodesFree)
|
2015-01-28 21:25:25 +08:00
|
|
|
ret.Used = (uint64(stat.Blocks) - uint64(stat.Bfree)) * uint64(bsize)
|
2017-02-02 07:05:29 +08:00
|
|
|
|
|
|
|
if ret.InodesTotal == 0 {
|
2016-12-23 15:50:28 +08:00
|
|
|
ret.InodesUsedPercent = 0
|
|
|
|
} else {
|
|
|
|
ret.InodesUsedPercent = (float64(ret.InodesUsed) / float64(ret.InodesTotal)) * 100.0
|
|
|
|
}
|
2014-04-18 15:34:47 +08:00
|
|
|
|
2018-07-30 20:59:23 +08:00
|
|
|
if (ret.Used + ret.Free) == 0 {
|
2016-12-23 15:50:28 +08:00
|
|
|
ret.UsedPercent = 0
|
|
|
|
} else {
|
2018-08-01 13:39:43 +08:00
|
|
|
// We don't use ret.Total to calculate percent.
|
|
|
|
// see https://github.com/shirou/gopsutil/issues/562
|
2018-07-30 20:59:23 +08:00
|
|
|
ret.UsedPercent = (float64(ret.Used) / float64(ret.Used+ret.Free)) * 100.0
|
2016-12-23 15:50:28 +08:00
|
|
|
}
|
2017-02-02 07:05:29 +08:00
|
|
|
|
2014-04-18 15:34:47 +08:00
|
|
|
return ret, nil
|
|
|
|
}
|
2018-07-24 06:24:05 +08:00
|
|
|
|
|
|
|
// Unescape escaped octal chars (like space 040, ampersand 046 and backslash 134) to their real value in fstab fields issue#555
|
|
|
|
func unescapeFstab(path string) string {
|
|
|
|
escaped, err := strconv.Unquote(`"` + path + `"`)
|
|
|
|
if err != nil {
|
|
|
|
return path
|
|
|
|
}
|
|
|
|
return escaped
|
|
|
|
}
|