summaryrefslogtreecommitdiffhomepage
path: root/pkg/syserr/host_linux.go
diff options
context:
space:
mode:
authorIan Gudger <igudger@google.com>2018-08-14 19:02:36 -0700
committerShentubot <shentubot@google.com>2018-08-14 19:03:38 -0700
commita620bea045b018b717fbba3193975e6d97c09bf9 (patch)
treee627299e37b463a00f1eb548725d12ac4662fbad /pkg/syserr/host_linux.go
parente8a4f2e133c3a7fb4a2dceb6675ebc57ea4f7350 (diff)
Reduce map lookups in syserr
PiperOrigin-RevId: 208755352 Change-Id: Ia24630f452a4a42940ab73a8113a2fd5ea2cfca2
Diffstat (limited to 'pkg/syserr/host_linux.go')
-rw-r--r--pkg/syserr/host_linux.go150
1 files changed, 18 insertions, 132 deletions
diff --git a/pkg/syserr/host_linux.go b/pkg/syserr/host_linux.go
index ffd78e8f8..22009a799 100644
--- a/pkg/syserr/host_linux.go
+++ b/pkg/syserr/host_linux.go
@@ -17,144 +17,30 @@
package syserr
import (
+ "fmt"
"syscall"
)
-var linuxHostTranslations = map[syscall.Errno]*Error{
- syscall.EPERM: ErrNotPermitted,
- syscall.ENOENT: ErrNoFileOrDir,
- syscall.ESRCH: ErrNoProcess,
- syscall.EINTR: ErrInterrupted,
- syscall.EIO: ErrIO,
- syscall.ENXIO: ErrDeviceOrAddress,
- syscall.E2BIG: ErrTooManyArgs,
- syscall.ENOEXEC: ErrEcec,
- syscall.EBADF: ErrBadFD,
- syscall.ECHILD: ErrNoChild,
- syscall.EAGAIN: ErrTryAgain,
- syscall.ENOMEM: ErrNoMemory,
- syscall.EACCES: ErrPermissionDenied,
- syscall.EFAULT: ErrBadAddress,
- syscall.ENOTBLK: ErrNotBlockDevice,
- syscall.EBUSY: ErrBusy,
- syscall.EEXIST: ErrExists,
- syscall.EXDEV: ErrCrossDeviceLink,
- syscall.ENODEV: ErrNoDevice,
- syscall.ENOTDIR: ErrNotDir,
- syscall.EISDIR: ErrIsDir,
- syscall.EINVAL: ErrInvalidArgument,
- syscall.ENFILE: ErrFileTableOverflow,
- syscall.EMFILE: ErrTooManyOpenFiles,
- syscall.ENOTTY: ErrNotTTY,
- syscall.ETXTBSY: ErrTestFileBusy,
- syscall.EFBIG: ErrFileTooBig,
- syscall.ENOSPC: ErrNoSpace,
- syscall.ESPIPE: ErrIllegalSeek,
- syscall.EROFS: ErrReadOnlyFS,
- syscall.EMLINK: ErrTooManyLinks,
- syscall.EPIPE: ErrBrokenPipe,
- syscall.EDOM: ErrDomain,
- syscall.ERANGE: ErrRange,
- syscall.EDEADLOCK: ErrDeadlock,
- syscall.ENAMETOOLONG: ErrNameTooLong,
- syscall.ENOLCK: ErrNoLocksAvailable,
- syscall.ENOSYS: ErrInvalidSyscall,
- syscall.ENOTEMPTY: ErrDirNotEmpty,
- syscall.ELOOP: ErrLinkLoop,
- syscall.ENOMSG: ErrNoMessage,
- syscall.EIDRM: ErrIdentifierRemoved,
- syscall.ECHRNG: ErrChannelOutOfRange,
- syscall.EL2NSYNC: ErrLevelTwoNotSynced,
- syscall.EL3HLT: ErrLevelThreeHalted,
- syscall.EL3RST: ErrLevelThreeReset,
- syscall.ELNRNG: ErrLinkNumberOutOfRange,
- syscall.EUNATCH: ErrProtocolDriverNotAttached,
- syscall.ENOCSI: ErrNoCSIAvailable,
- syscall.EL2HLT: ErrLevelTwoHalted,
- syscall.EBADE: ErrInvalidExchange,
- syscall.EBADR: ErrInvalidRequestDescriptor,
- syscall.EXFULL: ErrExchangeFull,
- syscall.ENOANO: ErrNoAnode,
- syscall.EBADRQC: ErrInvalidRequestCode,
- syscall.EBADSLT: ErrInvalidSlot,
- syscall.EBFONT: ErrBadFontFile,
- syscall.ENOSTR: ErrNotStream,
- syscall.ENODATA: ErrNoDataAvailable,
- syscall.ETIME: ErrTimerExpired,
- syscall.ENOSR: ErrStreamsResourceDepleted,
- syscall.ENONET: ErrMachineNotOnNetwork,
- syscall.ENOPKG: ErrPackageNotInstalled,
- syscall.EREMOTE: ErrIsRemote,
- syscall.ENOLINK: ErrNoLink,
- syscall.EADV: ErrAdvertise,
- syscall.ESRMNT: ErrSRMount,
- syscall.ECOMM: ErrSendCommunication,
- syscall.EPROTO: ErrProtocol,
- syscall.EMULTIHOP: ErrMultihopAttempted,
- syscall.EDOTDOT: ErrRFS,
- syscall.EBADMSG: ErrInvalidDataMessage,
- syscall.EOVERFLOW: ErrOverflow,
- syscall.ENOTUNIQ: ErrNetworkNameNotUnique,
- syscall.EBADFD: ErrFDInBadState,
- syscall.EREMCHG: ErrRemoteAddressChanged,
- syscall.ELIBACC: ErrSharedLibraryInaccessible,
- syscall.ELIBBAD: ErrCorruptedSharedLibrary,
- syscall.ELIBSCN: ErrLibSectionCorrupted,
- syscall.ELIBMAX: ErrTooManySharedLibraries,
- syscall.ELIBEXEC: ErrSharedLibraryExeced,
- syscall.EILSEQ: ErrIllegalByteSequence,
- syscall.ERESTART: ErrShouldRestart,
- syscall.ESTRPIPE: ErrStreamPipe,
- syscall.EUSERS: ErrTooManyUsers,
- syscall.ENOTSOCK: ErrNotASocket,
- syscall.EDESTADDRREQ: ErrDestinationAddressRequired,
- syscall.EMSGSIZE: ErrMessageTooLong,
- syscall.EPROTOTYPE: ErrWrongProtocolForSocket,
- syscall.ENOPROTOOPT: ErrProtocolNotAvailable,
- syscall.EPROTONOSUPPORT: ErrProtocolNotSupported,
- syscall.ESOCKTNOSUPPORT: ErrSocketNotSupported,
- syscall.EOPNOTSUPP: ErrEndpointOperation,
- syscall.EPFNOSUPPORT: ErrProtocolFamilyNotSupported,
- syscall.EAFNOSUPPORT: ErrAddressFamilyNotSupported,
- syscall.EADDRINUSE: ErrAddressInUse,
- syscall.EADDRNOTAVAIL: ErrAddressNotAvailable,
- syscall.ENETDOWN: ErrNetworkDown,
- syscall.ENETUNREACH: ErrNetworkUnreachable,
- syscall.ENETRESET: ErrNetworkReset,
- syscall.ECONNABORTED: ErrConnectionAborted,
- syscall.ECONNRESET: ErrConnectionReset,
- syscall.ENOBUFS: ErrNoBufferSpace,
- syscall.EISCONN: ErrAlreadyConnected,
- syscall.ENOTCONN: ErrNotConnected,
- syscall.ESHUTDOWN: ErrShutdown,
- syscall.ETOOMANYREFS: ErrTooManyRefs,
- syscall.ETIMEDOUT: ErrTimedOut,
- syscall.ECONNREFUSED: ErrConnectionRefused,
- syscall.EHOSTDOWN: ErrHostDown,
- syscall.EHOSTUNREACH: ErrNoRoute,
- syscall.EALREADY: ErrAlreadyInProgress,
- syscall.EINPROGRESS: ErrInProgress,
- syscall.ESTALE: ErrStaleFileHandle,
- syscall.EUCLEAN: ErrStructureNeedsCleaning,
- syscall.ENOTNAM: ErrIsNamedFile,
- syscall.EREMOTEIO: ErrRemoteIO,
- syscall.EDQUOT: ErrQuotaExceeded,
- syscall.ENOMEDIUM: ErrNoMedium,
- syscall.EMEDIUMTYPE: ErrWrongMediumType,
- syscall.ECANCELED: ErrCanceled,
- syscall.ENOKEY: ErrNoKey,
- syscall.EKEYEXPIRED: ErrKeyExpired,
- syscall.EKEYREVOKED: ErrKeyRevoked,
- syscall.EKEYREJECTED: ErrKeyRejected,
- syscall.EOWNERDEAD: ErrOwnerDied,
- syscall.ENOTRECOVERABLE: ErrNotRecoverable,
+const maxErrno = 134
+
+type linuxHostTranslation struct {
+ err *Error
+ ok bool
}
+var linuxHostTranslations [maxErrno]linuxHostTranslation
+
// FromHost translates a syscall.Errno to a corresponding Error value.
func FromHost(err syscall.Errno) *Error {
- e, ok := linuxHostTranslations[err]
- if !ok {
- panic("Unknown host errno " + err.Error())
+ if err < 0 || int(err) >= len(linuxHostTranslations) || !linuxHostTranslations[err].ok {
+ panic(fmt.Sprintf("unknown host errno %q (%d)", err.Error(), err))
+ }
+ return linuxHostTranslations[err].err
+}
+
+func addLinuxHostTranslation(host syscall.Errno, trans *Error) {
+ if linuxHostTranslations[host].ok {
+ panic(fmt.Sprintf("duplicate translation for host errno %q (%d)", host.Error(), host))
}
- return e
+ linuxHostTranslations[host] = linuxHostTranslation{err: trans, ok: true}
}