-
Notifications
You must be signed in to change notification settings - Fork 244
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
adds experimental sys.Errno to begin decoupling from the syscall pack…
…age (#1582) Signed-off-by: Adrian Cole <adrian@tetrate.io>
- Loading branch information
1 parent
1dafce0
commit 2f8dd23
Showing
94 changed files
with
1,583 additions
and
1,366 deletions.
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
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,95 @@ | ||
package sys | ||
|
||
import "strconv" | ||
|
||
// Errno is a subset of POSIX errno used by wazero interfaces. Zero is not an | ||
// error. Other values should not be interpreted numerically, rather by constants | ||
// prefixed with 'E'. | ||
// | ||
// See https://pubs.opengroup.org/onlinepubs/9699919799/basedefs/errno.h.html | ||
type Errno uint16 | ||
|
||
// ^-- Note: This will eventually move to the public /sys package. It is | ||
// experimental until we audit the socket related APIs to ensure we have all | ||
// the Errno it returns, and we export fs.FS. This is not in /internal/sys as | ||
// that would introduce a package cycle. | ||
|
||
// This is a subset of errors to reduce implementation burden. `wasip` defines | ||
// almost all POSIX error numbers, but not all are used in practice. wazero | ||
// will add ones needed in POSIX order, as needed by functions that explicitly | ||
// document returning them. | ||
// | ||
// https://github.com/WebAssembly/WASI/blob/snapshot-01/phases/snapshot/docs.md#-errno-enumu16 | ||
const ( | ||
EACCES Errno = iota + 1 | ||
EAGAIN | ||
EBADF | ||
EEXIST | ||
EFAULT | ||
EINTR | ||
EINVAL | ||
EIO | ||
EISDIR | ||
ELOOP | ||
ENAMETOOLONG | ||
ENOENT | ||
ENOSYS | ||
ENOTDIR | ||
ENOTEMPTY | ||
ENOTSOCK | ||
ENOTSUP | ||
EPERM | ||
EROFS | ||
|
||
// NOTE ENOTCAPABLE is defined in wasip1, but not in POSIX. wasi-libc | ||
// converts it to EBADF, ESPIPE or EINVAL depending on the call site. | ||
// It isn't known if compilers who don't use ENOTCAPABLE would crash on it. | ||
) | ||
|
||
// Error implements error | ||
func (e Errno) Error() string { | ||
switch e { | ||
case 0: // not an error | ||
return "success" | ||
case EACCES: | ||
return "permission denied" | ||
case EAGAIN: | ||
return "resource unavailable, try again" | ||
case EBADF: | ||
return "bad file descriptor" | ||
case EEXIST: | ||
return "file exists" | ||
case EFAULT: | ||
return "bad address" | ||
case EINTR: | ||
return "interrupted function" | ||
case EINVAL: | ||
return "invalid argument" | ||
case EIO: | ||
return "input/output error" | ||
case EISDIR: | ||
return "is a directory" | ||
case ELOOP: | ||
return "too many levels of symbolic links" | ||
case ENAMETOOLONG: | ||
return "filename too long" | ||
case ENOENT: | ||
return "no such file or directory" | ||
case ENOSYS: | ||
return "functionality not supported" | ||
case ENOTDIR: | ||
return "not a directory or a symbolic link to a directory" | ||
case ENOTEMPTY: | ||
return "directory not empty" | ||
case ENOTSOCK: | ||
return "not a socket" | ||
case ENOTSUP: | ||
return "not supported (may be the same value as [EOPNOTSUPP])" | ||
case EPERM: | ||
return "operation not permitted" | ||
case EROFS: | ||
return "read-only file system" | ||
default: | ||
return "Errno(" + strconv.Itoa(int(e)) + ")" | ||
} | ||
} |
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
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
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,98 @@ | ||
package sys | ||
|
||
import "syscall" | ||
|
||
func syscallToErrno(errno syscall.Errno) Errno { | ||
switch errno { | ||
case 0: | ||
return 0 | ||
case syscall.EACCES: | ||
return EACCES | ||
case syscall.EAGAIN: | ||
return EAGAIN | ||
case syscall.EBADF: | ||
return EBADF | ||
case syscall.EEXIST: | ||
return EEXIST | ||
case syscall.EFAULT: | ||
return EFAULT | ||
case syscall.EINTR: | ||
return EINTR | ||
case syscall.EINVAL: | ||
return EINVAL | ||
case syscall.EIO: | ||
return EIO | ||
case syscall.EISDIR: | ||
return EISDIR | ||
case syscall.ELOOP: | ||
return ELOOP | ||
case syscall.ENAMETOOLONG: | ||
return ENAMETOOLONG | ||
case syscall.ENOENT: | ||
return ENOENT | ||
case syscall.ENOSYS: | ||
return ENOSYS | ||
case syscall.ENOTDIR: | ||
return ENOTDIR | ||
case syscall.ENOTEMPTY: | ||
return ENOTEMPTY | ||
case syscall.ENOTSOCK: | ||
return ENOTSOCK | ||
case syscall.ENOTSUP: | ||
return ENOTSUP | ||
case syscall.EPERM: | ||
return EPERM | ||
case syscall.EROFS: | ||
return EROFS | ||
default: | ||
return EIO | ||
} | ||
} | ||
|
||
// Unwrap is a convenience for runtime.GOOS which define syscall.Errno. | ||
func (e Errno) Unwrap() error { | ||
switch e { | ||
case 0: | ||
return nil | ||
case EACCES: | ||
return syscall.EACCES | ||
case EAGAIN: | ||
return syscall.EAGAIN | ||
case EBADF: | ||
return syscall.EBADF | ||
case EEXIST: | ||
return syscall.EEXIST | ||
case EFAULT: | ||
return syscall.EFAULT | ||
case EINTR: | ||
return syscall.EINTR | ||
case EINVAL: | ||
return syscall.EINVAL | ||
case EIO: | ||
return syscall.EIO | ||
case EISDIR: | ||
return syscall.EISDIR | ||
case ELOOP: | ||
return syscall.ELOOP | ||
case ENAMETOOLONG: | ||
return syscall.ENAMETOOLONG | ||
case ENOENT: | ||
return syscall.ENOENT | ||
case ENOSYS: | ||
return syscall.ENOSYS | ||
case ENOTDIR: | ||
return syscall.ENOTDIR | ||
case ENOTEMPTY: | ||
return syscall.ENOTEMPTY | ||
case ENOTSOCK: | ||
return syscall.ENOTSOCK | ||
case ENOTSUP: | ||
return syscall.ENOTSUP | ||
case EPERM: | ||
return syscall.EPERM | ||
case EROFS: | ||
return syscall.EROFS | ||
default: | ||
return syscall.EIO | ||
} | ||
} |
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,16 @@ | ||
//go:build !windows | ||
|
||
package sys | ||
|
||
import "syscall" | ||
|
||
func errorToErrno(err error) Errno { | ||
switch err := err.(type) { | ||
case Errno: | ||
return err | ||
case syscall.Errno: | ||
return syscallToErrno(err) | ||
default: | ||
return EIO | ||
} | ||
} |
Oops, something went wrong.