sys: add support for NetBSD getvfsstat

NetBSD replaced the getfsstat interface with getvfsstat in NetBSD 3.0.

Add a test for it.

With help from Benny Siegert and Ian Lance Taylor.

Change-Id: I115ae48c287cc32181006e9e8f7c15851e7e36c0
Reviewed-on: https://go-review.googlesource.com/c/sys/+/550476
Reviewed-by: Cherry Mui <cherryyz@google.com>
Reviewed-by: Ian Lance Taylor <iant@golang.org>
Reviewed-by: Benny Siegert <bsiegert@gmail.com>
LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
Auto-Submit: Benny Siegert <bsiegert@gmail.com>
Auto-Submit: Ian Lance Taylor <iant@golang.org>
This commit is contained in:
Thomas Klausner
2023-12-16 21:21:14 +01:00
committed by Gopher Robot
parent b06ce0514e
commit 137f2eda35
2 changed files with 64 additions and 0 deletions

47
unix/getvfsstat_test.go Normal file
View File

@@ -0,0 +1,47 @@
// Copyright 2023 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
//go:build netbsd
package unix_test
import (
"os/exec"
"testing"
"golang.org/x/sys/unix"
)
func TestGetvfsstat(t *testing.T) {
n, err := unix.Getvfsstat(nil, unix.MNT_NOWAIT)
if err != nil {
t.Fatal(err)
}
data := make([]unix.Statvfs_t, n)
n2, err := unix.Getvfsstat(data, unix.MNT_NOWAIT)
if err != nil {
t.Fatal(err)
}
if n != n2 {
t.Errorf("Getvfsstat(nil) = %d, but subsequent Getvfsstat(slice) = %d", n, n2)
}
for i, stat := range data {
if stat == (unix.Statvfs_t{}) {
t.Errorf("index %v is an empty Statvfs_t struct", i)
}
t.Logf("%s on %s", unix.ByteSliceToString(stat.Mntfromname[:]), unix.ByteSliceToString(stat.Mntonname[:]))
}
if t.Failed() {
for i, stat := range data[:n2] {
t.Logf("data[%v] = %+v", i, stat)
}
mount, err := exec.Command("mount").CombinedOutput()
if err != nil {
t.Logf("mount: %v\n%s", err, mount)
} else {
t.Logf("mount: %s", mount)
}
}
}

View File

@@ -248,6 +248,23 @@ func Statvfs(path string, buf *Statvfs_t) (err error) {
return Statvfs1(path, buf, ST_WAIT)
}
func Getvfsstat(buf []Statvfs_t, flags int) (n int, err error) {
var (
_p0 unsafe.Pointer
bufsize uintptr
)
if len(buf) > 0 {
_p0 = unsafe.Pointer(&buf[0])
bufsize = unsafe.Sizeof(Statvfs_t{}) * uintptr(len(buf))
}
r0, _, e1 := Syscall(SYS_GETVFSSTAT, uintptr(_p0), bufsize, uintptr(flags))
n = int(r0)
if e1 != 0 {
err = e1
}
return
}
/*
* Exposed directly
*/