Spaces:
Sleeping
Sleeping
| // Copyright 2021 Google LLC | |
| // | |
| // Licensed under the Apache License, Version 2.0 (the "License"); | |
| // you may not use this file except in compliance with the License. | |
| // You may obtain a copy of the License at | |
| // | |
| // http://www.apache.org/licenses/LICENSE-2.0 | |
| // | |
| // Unless required by applicable law or agreed to in writing, software | |
| // distributed under the License is distributed on an "AS IS" BASIS, | |
| // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
| // See the License for the specific language governing permissions and | |
| // limitations under the License. | |
| namespace csrblocksparse { | |
| namespace { | |
| absl::StatusCode ErrnoToCode(int error_number) { | |
| switch (error_number) { | |
| case 0: | |
| return absl::StatusCode::kOk; | |
| case EINVAL: // Invalid argument | |
| case ENAMETOOLONG: // Filename too long | |
| case E2BIG: // Argument list too long | |
| case EDESTADDRREQ: // Destination address required | |
| case EDOM: // Mathematics argument out of domain of function | |
| case EFAULT: // Bad address | |
| case EILSEQ: // Illegal byte sequence | |
| case ENOPROTOOPT: // Protocol not available | |
| case ENOSTR: // Not a STREAM | |
| case ENOTSOCK: // Not a socket | |
| case ENOTTY: // Inappropriate I/O control operation | |
| case EPROTOTYPE: // Protocol wrong type for socket | |
| case ESPIPE: // Invalid seek | |
| return absl::StatusCode::kInvalidArgument; | |
| case ETIMEDOUT: // Connection timed out | |
| case ETIME: // Timer expired | |
| return absl::StatusCode::kDeadlineExceeded; | |
| case ENODEV: // No such device | |
| case ENOENT: // No such file or directory | |
| case ENOMEDIUM: // No medium found | |
| case ENXIO: // No such device or address | |
| case ESRCH: // No such process | |
| return absl::StatusCode::kNotFound; | |
| case EEXIST: // File exists | |
| case EADDRNOTAVAIL: // Address not available | |
| case EALREADY: // Connection already in progress | |
| case ENOTUNIQ: // Name not unique on network | |
| return absl::StatusCode::kAlreadyExists; | |
| case EPERM: // Operation not permitted | |
| case EACCES: // Permission denied | |
| case ENOKEY: // Required key not available | |
| case EROFS: // Read only file system | |
| return absl::StatusCode::kPermissionDenied; | |
| case ENOTEMPTY: // Directory not empty | |
| case EISDIR: // Is a directory | |
| case ENOTDIR: // Not a directory | |
| case EADDRINUSE: // Address already in use | |
| case EBADF: // Invalid file descriptor | |
| case EBADFD: // File descriptor in bad state | |
| case EBUSY: // Device or resource busy | |
| case ECHILD: // No child processes | |
| case EISCONN: // Socket is connected | |
| case EISNAM: // Is a named type file | |
| case ENOTBLK: // Block device required | |
| case ENOTCONN: // The socket is not connected | |
| case EPIPE: // Broken pipe | |
| case ESHUTDOWN: // Cannot send after transport endpoint shutdown | |
| case ETXTBSY: // Text file busy | |
| case EUNATCH: // Protocol driver not attached | |
| return absl::StatusCode::kFailedPrecondition; | |
| case ENOSPC: // No space left on device | |
| case EDQUOT: // Disk quota exceeded | |
| case EMFILE: // Too many open files | |
| case EMLINK: // Too many links | |
| case ENFILE: // Too many open files in system | |
| case ENOBUFS: // No buffer space available | |
| case ENODATA: // No message is available on the STREAM read queue | |
| case ENOMEM: // Not enough space | |
| case ENOSR: // No STREAM resources | |
| case EUSERS: // Too many users | |
| return absl::StatusCode::kResourceExhausted; | |
| case ECHRNG: // Channel number out of range | |
| case EFBIG: // File too large | |
| case EOVERFLOW: // Value too large to be stored in data type | |
| case ERANGE: // Result too large | |
| return absl::StatusCode::kOutOfRange; | |
| case ENOPKG: // Package not installed | |
| case ENOSYS: // Function not implemented | |
| case ENOTSUP: // Operation not supported | |
| case EAFNOSUPPORT: // Address family not supported | |
| case EPFNOSUPPORT: // Protocol family not supported | |
| case EPROTONOSUPPORT: // Protocol not supported | |
| case ESOCKTNOSUPPORT: // Socket type not supported | |
| case EXDEV: // Improper link | |
| return absl::StatusCode::kUnimplemented; | |
| case EAGAIN: // Resource temporarily unavailable | |
| case ECOMM: // Communication error on send | |
| case ECONNREFUSED: // Connection refused | |
| case ECONNABORTED: // Connection aborted | |
| case ECONNRESET: // Connection reset | |
| case EINTR: // Interrupted function call | |
| case EHOSTDOWN: // Host is down | |
| case EHOSTUNREACH: // Host is unreachable | |
| case ENETDOWN: // Network is down | |
| case ENETRESET: // Connection aborted by network | |
| case ENETUNREACH: // Network unreachable | |
| case ENOLCK: // No locks available | |
| case ENOLINK: // Link has been severed | |
| case ENONET: // Machine is not on the network | |
| return absl::StatusCode::kUnavailable; | |
| case EDEADLK: // Resource deadlock avoided | |
| case ESTALE: // Stale file handle | |
| return absl::StatusCode::kAborted; | |
| case ECANCELED: // Operation cancelled | |
| return absl::StatusCode::kCancelled; | |
| default: | |
| return absl::StatusCode::kUnknown; | |
| } | |
| } | |
| // POSIX `strerror_r()` returns `int`. | |
| ABSL_ATTRIBUTE_UNUSED std::string StrErrorResult(int result, const char* buffer, | |
| int error_code) { | |
| if (ABSL_PREDICT_FALSE(result != 0)) { | |
| return absl::StrCat("Unknown error ", error_code); | |
| } | |
| return buffer; | |
| } | |
| // GNU `strerror_r()` returns `char*`. | |
| ABSL_ATTRIBUTE_UNUSED std::string StrErrorResult(char* result, | |
| const char* buffer, | |
| int error_code) { | |
| return result; | |
| } | |
| std::string StrError(int error_code) { | |
| char message[256]; | |
| return StrErrorResult(strerror_r(error_code, message, sizeof(message)), | |
| message, error_code); | |
| } | |
| } // namespace | |
| absl::Status ErrnoToCanonicalStatus(int error_number, | |
| absl::string_view message) { | |
| return absl::Status(ErrnoToCode(error_number), | |
| absl::StrCat(message, ": ", StrError(error_number))); | |
| } | |
| } // namespace csrblocksparse | |