diff --git a/patches/gopsutil/v3/disk/disk_darwin.go b/patches/gopsutil/v3/disk/disk_darwin.go index 0877b76114eac..bbdf748482e7e 100644 --- a/patches/gopsutil/v3/disk/disk_darwin.go +++ b/patches/gopsutil/v3/disk/disk_darwin.go @@ -5,6 +5,10 @@ package disk import ( "context" + "fmt" + "os/exec" + "strconv" + "strings" "github.com/shirou/gopsutil/v3/internal/common" "golang.org/x/sys/unix" @@ -85,3 +89,69 @@ func SerialNumberWithContext(ctx context.Context, name string) (string, error) { func LabelWithContext(ctx context.Context, name string) (string, error) { return "", common.ErrNotImplementedError } + +func UsageWithContext(ctx context.Context, path string) (*UsageStat, error) { + cmd := exec.Command("/bin/df", "-ki", path) + output, err := cmd.Output() + if err != nil { + return nil, fmt.Errorf("error executing df command: %v", err) + } + + lines := strings.Split(strings.TrimSpace(string(output)), "\n") + if len(lines) < 2 { + return nil, fmt.Errorf("unexpected df output format") + } + + // Skip the header line and process the data line + fields := strings.Fields(lines[1]) + if len(fields) < 9 { + return nil, fmt.Errorf("unexpected number of fields in df output") + } + + // Parse values + total, err := parseUint64(fields[1]) + if err != nil { + return nil, fmt.Errorf("error parsing total blocks: %v", err) + } + used, err := parseUint64(fields[2]) + if err != nil { + return nil, fmt.Errorf("error parsing used blocks: %v", err) + } + free, err := parseUint64(fields[3]) + if err != nil { + return nil, fmt.Errorf("error parsing available blocks: %v", err) + } + inodesUsed, err := parseUint64(fields[5]) + if err != nil { + return nil, fmt.Errorf("error parsing iused: %v", err) + } + inodesFree, err := parseUint64(fields[6]) + if err != nil { + return nil, fmt.Errorf("error parsing ifree: %v", err) + } + + // Calculate percentages + usedPercent := float64(used) / float64(total) * 100 + inodesTotal := inodesUsed + inodesFree + inodesUsedPercent := float64(inodesUsed) / float64(inodesTotal) * 100 + + // Create UsageStat object + us := &UsageStat{ + Path: fields[8], + Fstype: fields[0], + Total: total * 1024, + Free: free * 1024, + Used: used * 1024, + UsedPercent: usedPercent, + InodesTotal: inodesTotal, + InodesUsed: inodesUsed, + InodesFree: inodesFree, + InodesUsedPercent: inodesUsedPercent, + } + + return us, nil +} + +func parseUint64(s string) (uint64, error) { + return strconv.ParseUint(s, 10, 64) +} diff --git a/patches/gopsutil/v3/disk/disk_unix.go b/patches/gopsutil/v3/disk/disk_unix.go index bdb62b24d6d69..5df6699578005 100644 --- a/patches/gopsutil/v3/disk/disk_unix.go +++ b/patches/gopsutil/v3/disk/disk_unix.go @@ -1,5 +1,5 @@ -//go:build freebsd || linux || darwin -// +build freebsd linux darwin +//go:build freebsd || linux +// +build freebsd linux package disk