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

     1  package sys
     2  
     3  import "syscall"
     4  
     5  // These are errors not defined in the syscall package. They are prefixed with
     6  // underscore to avoid exporting them.
     7  //
     8  // See https://learn.microsoft.com/en-us/windows/win32/debug/system-error-codes--0-499-
     9  const (
    10  	// _ERROR_INVALID_HANDLE is a Windows error returned by syscall.Write
    11  	// instead of syscall.EBADF
    12  	_ERROR_INVALID_HANDLE = syscall.Errno(6)
    13  
    14  	// _ERROR_INVALID_NAME is a Windows error returned by open when a file
    15  	// path has a trailing slash
    16  	_ERROR_INVALID_NAME = syscall.Errno(0x7B)
    17  
    18  	// _ERROR_NEGATIVE_SEEK is a Windows error returned by os.Truncate
    19  	// instead of syscall.EINVAL
    20  	_ERROR_NEGATIVE_SEEK = syscall.Errno(0x83)
    21  
    22  	// _ERROR_DIRECTORY is a Windows error returned by syscall.Rmdir
    23  	// instead of syscall.ENOTDIR
    24  	_ERROR_DIRECTORY = syscall.Errno(0x10B)
    25  
    26  	// _ERROR_INVALID_SOCKET is a Windows error returned by winsock_select
    27  	// when a given handle is not a socket.
    28  	_ERROR_INVALID_SOCKET = syscall.Errno(0x2736)
    29  )
    30  
    31  func errorToErrno(err error) Errno {
    32  	switch err := err.(type) {
    33  	case Errno:
    34  		return err
    35  	case syscall.Errno:
    36  		// Note: In windows, _ERROR_PATH_NOT_FOUND(0x3) maps to syscall.ENOTDIR
    37  		switch err {
    38  		case syscall.ERROR_ALREADY_EXISTS:
    39  			return EEXIST
    40  		case _ERROR_DIRECTORY:
    41  			return ENOTDIR
    42  		case syscall.ERROR_DIR_NOT_EMPTY:
    43  			return ENOTEMPTY
    44  		case syscall.ERROR_FILE_EXISTS:
    45  			return EEXIST
    46  		case _ERROR_INVALID_HANDLE, _ERROR_INVALID_SOCKET:
    47  			return EBADF
    48  		case syscall.ERROR_ACCESS_DENIED:
    49  			// POSIX read and write functions expect EBADF, not EACCES when not
    50  			// open for reading or writing.
    51  			return EBADF
    52  		case syscall.ERROR_PRIVILEGE_NOT_HELD:
    53  			return EPERM
    54  		case _ERROR_NEGATIVE_SEEK, _ERROR_INVALID_NAME:
    55  			return EINVAL
    56  		}
    57  		errno, _ := syscallToErrno(err)
    58  		return errno
    59  	default:
    60  		return EIO
    61  	}
    62  }