From 137f2eda356924019c101f440bbbc0270ddd857f Mon Sep 17 00:00:00 2001 From: Thomas Klausner Date: Sat, 16 Dec 2023 21:21:14 +0100 Subject: [PATCH] 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 Reviewed-by: Ian Lance Taylor Reviewed-by: Benny Siegert LUCI-TryBot-Result: Go LUCI Auto-Submit: Benny Siegert Auto-Submit: Ian Lance Taylor --- unix/getvfsstat_test.go | 47 +++++++++++++++++++++++++++++++++++++++++ unix/syscall_netbsd.go | 17 +++++++++++++++ 2 files changed, 64 insertions(+) create mode 100644 unix/getvfsstat_test.go diff --git a/unix/getvfsstat_test.go b/unix/getvfsstat_test.go new file mode 100644 index 00000000..30c963a1 --- /dev/null +++ b/unix/getvfsstat_test.go @@ -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) + } + } +} diff --git a/unix/syscall_netbsd.go b/unix/syscall_netbsd.go index 88162099..34a46769 100644 --- a/unix/syscall_netbsd.go +++ b/unix/syscall_netbsd.go @@ -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 */