lbry.go/util/disk_space.go
Niko Storni 788857d2c3
add disk space check
remove unneeded panics
improve error handling
add tests
fix error checking that was comparing for exact values
sort imports
2018-07-12 08:24:43 -04:00

18 lines
532 B
Go

package util
import "syscall"
// GetUsedSpace returns a value between 0 and 1, with 0 being completely empty and 1 being full, for the disk that holds the provided path
func GetUsedSpace(path string) (float32, error) {
var stat syscall.Statfs_t
err := syscall.Statfs(path, &stat)
if err != nil {
return 0, err
}
// Available blocks * size per block = available space in bytes
all := stat.Blocks * uint64(stat.Bsize)
free := stat.Bfree * uint64(stat.Bsize)
used := all - free
return float32(used) / float32(all), nil
}