Skip to content

Commit

Permalink
use df command to get disk usage data on darwin
Browse files Browse the repository at this point in the history
  • Loading branch information
movence committed Jan 9, 2025
1 parent 6db0718 commit 9f56d5c
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 2 deletions.
70 changes: 70 additions & 0 deletions patches/gopsutil/v3/disk/disk_darwin.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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)
}
4 changes: 2 additions & 2 deletions patches/gopsutil/v3/disk/disk_unix.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
//go:build freebsd || linux || darwin
// +build freebsd linux darwin
//go:build freebsd || linux
// +build freebsd linux

package disk

Expand Down

0 comments on commit 9f56d5c

Please sign in to comment.