diskcheck.go 960 B

1234567891011121314151617181920212223242526272829303132333435
  1. //go:build !windows && !solaris && !netbsd && !openbsd && !js
  2. // +build !windows,!solaris,!netbsd,!openbsd,!js
  3. package healthcheck
  4. import "syscall"
  5. // AvailableDiskSpaceRatio returns ratio of available disk space to total
  6. // capacity.
  7. func AvailableDiskSpaceRatio(path string) (float64, error) {
  8. s := syscall.Statfs_t{}
  9. err := syscall.Statfs(path, &s)
  10. if err != nil {
  11. return 0, err
  12. }
  13. // Calculate our free blocks/total blocks to get our total ratio of
  14. // free blocks.
  15. return float64(s.Bfree) / float64(s.Blocks), nil
  16. }
  17. // AvailableDiskSpace returns the available disk space in bytes of the given
  18. // file system.
  19. func AvailableDiskSpace(path string) (uint64, error) {
  20. s := syscall.Statfs_t{}
  21. err := syscall.Statfs(path, &s)
  22. if err != nil {
  23. return 0, err
  24. }
  25. // Some OSes have s.Bavail defined as int64, others as uint64, so we
  26. // need the explicit type conversion here.
  27. return uint64(s.Bavail) * uint64(s.Bsize), nil // nolint:unconvert
  28. }