Files
wazero/internal/platform/error.go
Crypt Keeper 36bf277534 sysfs: requires all methods to return syscall.Errno (#1264)
This forces all syscall functions, notably filesystem, to return numeric
codes as opposed to mapping in two different areas. The result of this
change is better consolidation in call sites of `sysfs.FS`, while
further refactoring is needed to address consolidation of file errors.

Signed-off-by: Adrian Cole <adrian@tetrate.io>
2023-03-22 07:47:57 +01:00

53 lines
1.0 KiB
Go

package platform
import (
"io"
"io/fs"
"os"
"syscall"
)
// UnwrapOSError returns a syscall.Errno or zero if the input is nil.
func UnwrapOSError(err error) syscall.Errno {
if err == nil {
return 0
}
err = underlyingError(err)
if se, ok := err.(syscall.Errno); ok {
return adjustErrno(se)
}
// Below are all the fs.ErrXXX in fs.go.
//
// Note: Once we have our own file type, we should never see these.
switch err {
case nil, io.EOF:
return 0
case fs.ErrInvalid:
return syscall.EINVAL
case fs.ErrPermission:
return syscall.EPERM
case fs.ErrExist:
return syscall.EEXIST
case fs.ErrNotExist:
return syscall.ENOENT
case fs.ErrClosed:
return syscall.EBADF
}
return syscall.EIO
}
// underlyingError returns the underlying error if a well-known OS error type.
//
// This impl is basically the same as os.underlyingError in os/error.go
func underlyingError(err error) error {
switch err := err.(type) {
case *os.PathError:
return err.Err
case *os.LinkError:
return err.Err
case *os.SyscallError:
return err.Err
}
return err
}