github.com/decred/dcrlnd@v0.7.6/healthcheck/diskcheck.go (about)

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