Skip to content

Commit

Permalink
Merge pull request #5632 from crazy-max/disk-bsd-fix
Browse files Browse the repository at this point in the history
util(disk): freebsd and netbsd support
  • Loading branch information
crazy-max authored Jan 8, 2025
2 parents 4c68785 + 5c75264 commit d995796
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 1 deletion.
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

0 comments on commit d995796

Please sign in to comment.