-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #5632 from crazy-max/disk-bsd-fix
util(disk): freebsd and netbsd support
- Loading branch information
Showing
4 changed files
with
59 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
|
||
|