37 lines
842 B
Go
37 lines
842 B
Go
//go:build !windows
|
|
|
|
package host
|
|
|
|
import (
|
|
"os"
|
|
"syscall"
|
|
)
|
|
|
|
// Drive is a disk a data folder could go on.
|
|
type Drive struct {
|
|
Root string `json:"root"`
|
|
FreeGB float64 `json:"free_gb"`
|
|
System bool `json:"system"`
|
|
External bool `json:"external"`
|
|
VolumeGUID string `json:"volume_guid,omitempty"`
|
|
}
|
|
|
|
// ListDrives returns the home directory's volume on dev/preview hosts.
|
|
func ListDrives() []Drive {
|
|
home, err := os.UserHomeDir()
|
|
if err != nil {
|
|
return nil
|
|
}
|
|
free, _ := FreeGB(home)
|
|
return []Drive{{Root: home, FreeGB: free, System: true}}
|
|
}
|
|
|
|
// FreeGB is the free space on the volume holding path.
|
|
func FreeGB(path string) (float64, bool) {
|
|
var st syscall.Statfs_t
|
|
if err := syscall.Statfs(path, &st); err != nil {
|
|
return 0, false
|
|
}
|
|
return float64(st.Bavail) * float64(st.Bsize) / (1 << 30), true
|
|
}
|