//go:build unix package stats import ( "fmt" "os" "syscall" ) // DiskUsage returns the space on the file systems holding paths (label -> // directory), one entry per file system with the labels of the paths on // it, in the order of labels. func DiskUsage(labels []string, paths map[string]string) []Disk { var out []Disk byDev := map[string]int{} for _, l := range labels { p := paths[l] fi, err := os.Stat(p) if err != nil { continue } dev := fmt.Sprint(p) if st, ok := fi.Sys().(*syscall.Stat_t); ok { dev = fmt.Sprint(st.Dev) } if i, ok := byDev[dev]; ok { out[i].Holds = append(out[i].Holds, l) continue } var fs syscall.Statfs_t if err := syscall.Statfs(p, &fs); err != nil { continue } byDev[dev] = len(out) out = append(out, Disk{Path: p, Holds: []string{l}, Total: int64(fs.Blocks) * int64(fs.Bsize), Free: int64(fs.Bavail) * int64(fs.Bsize)}) } return out }