From 579d34c39c6d26661403eb210850686a677080b2 Mon Sep 17 00:00:00 2001 From: Nate Maninger Date: Tue, 16 Apr 2024 14:12:44 -0700 Subject: [PATCH 1/2] disk: support openbsd --- internal/disk/usage_default.go | 2 +- internal/disk/usage_openbsd.go | 20 ++++++++++++++++++++ 2 files changed, 21 insertions(+), 1 deletion(-) create mode 100644 internal/disk/usage_openbsd.go diff --git a/internal/disk/usage_default.go b/internal/disk/usage_default.go index 68b4ab53..4b0e27c0 100644 --- a/internal/disk/usage_default.go +++ b/internal/disk/usage_default.go @@ -1,4 +1,4 @@ -//go:build !windows +//go:build !windows && !openbsd package disk diff --git a/internal/disk/usage_openbsd.go b/internal/disk/usage_openbsd.go new file mode 100644 index 00000000..b53ee23d --- /dev/null +++ b/internal/disk/usage_openbsd.go @@ -0,0 +1,20 @@ +//go:build openbsd + +package disk + +import "golang.org/x/sys/unix" + +// Usage returns the free and total bytes on the filesystem containing the +// specified path. +func Usage(p string) (free, total uint64, err error) { + var stat unix.Statfs_t + if err := unix.Statfs(p, &stat); err != nil { + return 0, 0, err + } + return stat.F_bfree * uint64(stat.F_bsize), stat.F_blocks * uint64(stat.F_bsize), nil +} + +// Drives returns the paths of all drives on Windows. It is a no-op on other systems +func Drives() ([]string, error) { + return nil, nil +} From fcf2857a2a2fc42082da6d3f3a7f85a3307d77d2 Mon Sep 17 00:00:00 2001 From: Nate Maninger Date: Wed, 17 Apr 2024 13:08:56 -0700 Subject: [PATCH 2/2] disk: add comment about syscall differences --- internal/disk/usage_default.go | 3 +++ internal/disk/usage_openbsd.go | 3 +++ 2 files changed, 6 insertions(+) diff --git a/internal/disk/usage_default.go b/internal/disk/usage_default.go index 4b0e27c0..748bc176 100644 --- a/internal/disk/usage_default.go +++ b/internal/disk/usage_default.go @@ -11,6 +11,9 @@ func Usage(p string) (free, total uint64, err error) { if err := unix.Statfs(p, &stat); err != nil { return 0, 0, err } + // Linux-based systems and Darwin use bfree and blocks to represent the + // number of free and total blocks in the filesystem. Multiplying by f_bsize + // converts that to the number of free and total bytes of a filesystem. return stat.Bfree * uint64(stat.Bsize), stat.Blocks * uint64(stat.Bsize), nil } diff --git a/internal/disk/usage_openbsd.go b/internal/disk/usage_openbsd.go index b53ee23d..8411fc7e 100644 --- a/internal/disk/usage_openbsd.go +++ b/internal/disk/usage_openbsd.go @@ -11,6 +11,9 @@ func Usage(p string) (free, total uint64, err error) { if err := unix.Statfs(p, &stat); err != nil { return 0, 0, err } + // OpenBSD uses f_bfree and f_blocks to represent the number of free and + // total blocks in the filesystem. Multiplying by f_bsize converts that to + // the number of free and total bytes of a filesystem. return stat.F_bfree * uint64(stat.F_bsize), stat.F_blocks * uint64(stat.F_bsize), nil }