diskusage_unix.go 421 B

123456789101112131415
  1. //go:build linux || darwin || freebsd
  2. package capability
  3. import "golang.org/x/sys/unix"
  4. // DiskUsage returns the free and total bytes of the file system holding path.
  5. func DiskUsage(path string) (free int64, total int64, err error) {
  6. var st unix.Statfs_t
  7. if err := unix.Statfs(path, &st); err != nil {
  8. return 0, 0, err
  9. }
  10. bsize := int64(st.Bsize)
  11. return int64(st.Bavail) * bsize, int64(st.Blocks) * bsize, nil
  12. }