Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

util(disk): freebsd and netbsd support #5632

Merged
merged 2 commits into from
Jan 8, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions util/disk/disk_freebsd.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
//go:build freebsd

package disk

import (
"syscall"

"github.com/pkg/errors"
)

func GetDiskStat(root string) (DiskStat, error) {
var st syscall.Statfs_t
if err := syscall.Statfs(root, &st); err != nil {
return DiskStat{}, errors.Wrapf(err, "could not stat fs at %s", root)
}

return DiskStat{
Total: int64(st.Bsize) * int64(st.Blocks),
Free: int64(st.Bsize) * int64(st.Bfree),
Available: int64(st.Bsize) * int64(st.Bavail),
}, nil
}
21 changes: 21 additions & 0 deletions util/disk/disk_netbsd.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
//go:build netbsd

package disk

import (
"github.com/pkg/errors"
"golang.org/x/sys/unix"
)

func GetDiskStat(root string) (DiskStat, error) {
var st unix.Statvfs_t
if err := unix.Statvfs(root, &st); err != nil {
return DiskStat{}, errors.Wrapf(err, "could not stat fs at %s", root)
}

return DiskStat{
Total: int64(st.Frsize) * int64(st.Blocks),
Free: int64(st.Frsize) * int64(st.Bfree),
Available: int64(st.Frsize) * int64(st.Bavail),
}, nil
}
15 changes: 15 additions & 0 deletions util/disk/disk_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package disk

import (
"testing"

"github.com/stretchr/testify/require"
)

func TestGetDiskStat(t *testing.T) {
diskStat, err := GetDiskStat("/")
require.NoError(t, err)
require.Greater(t, diskStat.Total, int64(0))
require.GreaterOrEqual(t, diskStat.Free, int64(0))
require.GreaterOrEqual(t, diskStat.Available, int64(0))
}
2 changes: 1 addition & 1 deletion util/disk/disk_unix.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
//go:build !windows && !openbsd
//go:build !windows && !freebsd && !netbsd && !openbsd

package disk

Expand Down
Loading