github.com/wasilibs/wazerox@v0.0.0-20240124024944-4923be63ab5f/experimental/sys/syscall_errno.go (about)

     1  //go:build !plan9 && !aix
     2  
     3  package sys
     4  
     5  import "syscall"
     6  
     7  func syscallToErrno(err error) (Errno, bool) {
     8  	errno, ok := err.(syscall.Errno)
     9  	if !ok {
    10  		return 0, false
    11  	}
    12  	switch errno {
    13  	case 0:
    14  		return 0, true
    15  	case syscall.EACCES:
    16  		return EACCES, true
    17  	case syscall.EAGAIN:
    18  		return EAGAIN, true
    19  	case syscall.EBADF:
    20  		return EBADF, true
    21  	case syscall.EEXIST:
    22  		return EEXIST, true
    23  	case syscall.EFAULT:
    24  		return EFAULT, true
    25  	case syscall.EINTR:
    26  		return EINTR, true
    27  	case syscall.EINVAL:
    28  		return EINVAL, true
    29  	case syscall.EIO:
    30  		return EIO, true
    31  	case syscall.EISDIR:
    32  		return EISDIR, true
    33  	case syscall.ELOOP:
    34  		return ELOOP, true
    35  	case syscall.ENAMETOOLONG:
    36  		return ENAMETOOLONG, true
    37  	case syscall.ENOENT:
    38  		return ENOENT, true
    39  	case syscall.ENOSYS:
    40  		return ENOSYS, true
    41  	case syscall.ENOTDIR:
    42  		return ENOTDIR, true
    43  	case syscall.ERANGE:
    44  		return ERANGE, true
    45  	case syscall.ENOTEMPTY:
    46  		return ENOTEMPTY, true
    47  	case syscall.ENOTSOCK:
    48  		return ENOTSOCK, true
    49  	case syscall.ENOTSUP:
    50  		return ENOTSUP, true
    51  	case syscall.EPERM:
    52  		return EPERM, true
    53  	case syscall.EROFS:
    54  		return EROFS, true
    55  	default:
    56  		return EIO, true
    57  	}
    58  }
    59  
    60  // Unwrap is a convenience for runtime.GOOS which define syscall.Errno.
    61  func (e Errno) Unwrap() error {
    62  	switch e {
    63  	case 0:
    64  		return nil
    65  	case EACCES:
    66  		return syscall.EACCES
    67  	case EAGAIN:
    68  		return syscall.EAGAIN
    69  	case EBADF:
    70  		return syscall.EBADF
    71  	case EEXIST:
    72  		return syscall.EEXIST
    73  	case EFAULT:
    74  		return syscall.EFAULT
    75  	case EINTR:
    76  		return syscall.EINTR
    77  	case EINVAL:
    78  		return syscall.EINVAL
    79  	case EIO:
    80  		return syscall.EIO
    81  	case EISDIR:
    82  		return syscall.EISDIR
    83  	case ELOOP:
    84  		return syscall.ELOOP
    85  	case ENAMETOOLONG:
    86  		return syscall.ENAMETOOLONG
    87  	case ENOENT:
    88  		return syscall.ENOENT
    89  	case ENOSYS:
    90  		return syscall.ENOSYS
    91  	case ENOTDIR:
    92  		return syscall.ENOTDIR
    93  	case ENOTEMPTY:
    94  		return syscall.ENOTEMPTY
    95  	case ENOTSOCK:
    96  		return syscall.ENOTSOCK
    97  	case ENOTSUP:
    98  		return syscall.ENOTSUP
    99  	case EPERM:
   100  		return syscall.EPERM
   101  	case EROFS:
   102  		return syscall.EROFS
   103  	default:
   104  		return syscall.EIO
   105  	}
   106  }