sysfs: adds FS.Stat and companions in platform (#1140)

This centralizes filestat logic by making our own `Stat_t` similar to
`syscall.Stat_t`. This exposes utilities in the platform package and
adds a new function `FS.Stat` which avoids having to use `fs.File` to
get the same info. Doing so at the FS abstraction allows us to optimize
how it is implemented internally using portable means (e.g.
`os.StatFile`) or OS-specific means where necessary, e.g. in windows.

This also ensures `platform.OpenFile` returns syscall.Errno and
centralizes error checking with a new `require.EqualErrno` test.

Signed-off-by: Adrian Cole <adrian@tetrate.io>
This commit is contained in:
Crypt Keeper
2023-02-21 10:13:37 +08:00
committed by GitHub
parent 2587989f0a
commit 4ca0858e57
48 changed files with 967 additions and 679 deletions

View File

@@ -13,6 +13,7 @@ import (
"reflect"
"runtime"
"strings"
"syscall"
"unicode"
"unicode/utf8"
)
@@ -126,6 +127,19 @@ func Error(t TestingT, err error, formatWithArgs ...interface{}) {
}
}
// EqualErrno should be used for functions that return syscall.Errno or nil.
func EqualErrno(t TestingT, expected syscall.Errno, err error, formatWithArgs ...interface{}) {
if err == nil {
fail(t, "expected a syscall.Errno, but was nil", "", formatWithArgs...)
return
}
if se, ok := err.(syscall.Errno); !ok {
fail(t, fmt.Sprintf("expected %v to be a syscall.Errno", err), "", formatWithArgs...)
} else if se != expected {
fail(t, fmt.Sprintf("expected Errno %#[1]v(%[1]s), but was %#[2]v(%[2]s)", expected, err), "", formatWithArgs...)
}
}
// ErrorIs fails if the err is nil or errors.Is fails against the expected.
//
// - formatWithArgs are optional. When the first is a string that contains '%', it is treated like fmt.Sprintf.