|
8 | 8 |
|
9 | 9 | #if os(OSX) || os(iOS) || os(watchOS) || os(tvOS) |
10 | 10 | import Darwin.C |
| 11 | + import Foundation |
11 | 12 | #elseif os(Linux) |
12 | 13 | import Glibc |
13 | 14 | #endif |
14 | 15 |
|
| 16 | +#if os(OSX) || os(iOS) || os(watchOS) || os(tvOS) |
| 17 | + |
| 18 | +#endif |
| 19 | + |
15 | 20 | public extension POSIXError { |
16 | 21 |
|
17 | 22 | /// Creates error from C ```errno```. |
18 | | - static var fromErrno: POSIXError? { return self.init(rawValue: errno) } |
| 23 | + static var fromErrno: POSIXError? { |
| 24 | + |
| 25 | + guard let code = POSIXErrorCode(rawValue: errno) |
| 26 | + else { return nil } |
| 27 | + |
| 28 | + return self.init(code: code) |
| 29 | + } |
| 30 | + |
| 31 | + /// Creates `POSIXError` from error code. |
| 32 | + @inline(__always) |
| 33 | + init(code: POSIXErrorCode) { |
| 34 | + |
| 35 | + let nsError = NSError(domain: NSPOSIXErrorDomain, code: Int(code.rawValue), userInfo: nil) |
| 36 | + |
| 37 | + self.init(_nsError: nsError) |
| 38 | + } |
19 | 39 | } |
20 | 40 |
|
21 | 41 | #if os(Linux) |
22 | 42 |
|
23 | 43 | /// Enumeration describing POSIX error codes. |
24 | | - public enum POSIXError: ErrorProtocol, RawRepresentable { |
| 44 | + public struct POSIXError: Error { |
25 | 45 |
|
26 | | - case Value(CInt) |
| 46 | + let code: POSIXErrorCode |
27 | 47 |
|
28 | | - public init?(rawValue: CInt) { |
29 | | - |
30 | | - guard rawValue != 0 |
31 | | - else { return nil } |
| 48 | + public init(code: POSIXErrorCode) { |
32 | 49 |
|
33 | | - self = .Value(rawValue) |
| 50 | + self.code = code |
34 | 51 | } |
| 52 | + } |
| 53 | + |
| 54 | + /// Enumeration describing POSIX error codes. |
| 55 | + public enum POSIXErrorCode : Int32 { |
35 | 56 |
|
36 | | - public var rawValue: CInt { |
37 | | - |
38 | | - switch self { |
39 | | - |
40 | | - case let .Value(rawValue): return rawValue |
41 | | - } |
42 | | - } |
| 57 | + /// Operation not permitted. |
| 58 | + case EPERM = 1 |
| 59 | + /// No such file or directory. |
| 60 | + case ENOENT = 2 |
| 61 | + /// No such process. |
| 62 | + case ESRCH = 3 |
| 63 | + /// Interrupted system call. |
| 64 | + case EINTR = 4 |
| 65 | + /// Input/output error. |
| 66 | + case EIO = 5 |
| 67 | + /// Device not configured. |
| 68 | + case ENXIO = 6 |
| 69 | + /// Argument list too long. |
| 70 | + case E2BIG = 7 |
| 71 | + /// Exec format error. |
| 72 | + case ENOEXEC = 8 |
| 73 | + /// Bad file descriptor. |
| 74 | + case EBADF = 9 |
| 75 | + /// No child processes. |
| 76 | + case ECHILD = 10 |
| 77 | + /// Try again. |
| 78 | + case EAGAIN = 11 |
| 79 | + /// Cannot allocate memory. |
| 80 | + case ENOMEM = 12 |
| 81 | + /// Permission denied. |
| 82 | + case EACCES = 13 |
| 83 | + /// Bad address. |
| 84 | + case EFAULT = 14 |
| 85 | + /// Block device required. |
| 86 | + case ENOTBLK = 15 |
| 87 | + /// Device / Resource busy. |
| 88 | + case EBUSY = 16 |
| 89 | + /// File exists. |
| 90 | + case EEXIST = 17 |
| 91 | + /// Cross-device link. |
| 92 | + case EXDEV = 18 |
| 93 | + /// Operation not supported by device. |
| 94 | + case ENODEV = 19 |
| 95 | + /// Not a directory. |
| 96 | + case ENOTDIR = 20 |
| 97 | + /// Is a directory. |
| 98 | + case EISDIR = 21 |
| 99 | + /// Invalid argument. |
| 100 | + case EINVAL = 22 |
| 101 | + /// Too many open files in system. |
| 102 | + case ENFILE = 23 |
| 103 | + /// Too many open files. |
| 104 | + case EMFILE = 24 |
| 105 | + /// Inappropriate ioctl for device. |
| 106 | + case ENOTTY = 25 |
| 107 | + /// Text file busy. |
| 108 | + case ETXTBSY = 26 |
| 109 | + /// File too large. |
| 110 | + case EFBIG = 27 |
| 111 | + /// No space left on device. |
| 112 | + case ENOSPC = 28 |
| 113 | + /// Illegal seek. |
| 114 | + case ESPIPE = 29 |
| 115 | + /// Read-only file system. |
| 116 | + case EROFS = 30 |
| 117 | + /// Too many links. |
| 118 | + case EMLINK = 31 |
| 119 | + /// Broken pipe. |
| 120 | + case EPIPE = 32 |
| 121 | + |
| 122 | + /// math software. |
| 123 | + /// Numerical argument out of domain. |
| 124 | + case EDOM = 33 |
| 125 | + /// Result too large. |
| 126 | + case ERANGE = 34 |
| 127 | + |
| 128 | + /// Resource deadlock would occur. |
| 129 | + case EDEADLK = 35 |
| 130 | + |
| 131 | + /// File name too long. |
| 132 | + case ENAMETOOLONG = 36 |
| 133 | + |
| 134 | + /// No record locks available |
| 135 | + case ENOLCK = 37 |
| 136 | + |
| 137 | + /// Function not implemented. |
| 138 | + case ENOSYS = 38 |
| 139 | + |
| 140 | + /// Directory not empty. |
| 141 | + case ENOTEMPTY = 39 |
| 142 | + |
| 143 | + /// Too many symbolic links encountered |
| 144 | + case ELOOP = 40 |
| 145 | + |
| 146 | + /// Operation would block. |
| 147 | + public static var EWOULDBLOCK: POSIXErrorCode { return .EAGAIN } |
| 148 | + |
| 149 | + /// No message of desired type. |
| 150 | + case ENOMSG = 42 |
| 151 | + |
| 152 | + /// Identifier removed. |
| 153 | + case EIDRM = 43 |
| 154 | + |
| 155 | + /// Channel number out of range. |
| 156 | + case ECHRNG = 44 |
| 157 | + |
| 158 | + /// Level 2 not synchronized. |
| 159 | + case EL2NSYNC = 45 |
| 160 | + |
| 161 | + /// Level 3 halted |
| 162 | + case EL3HLT = 46 |
| 163 | + |
| 164 | + /// Level 3 reset. |
| 165 | + case EL3RST = 47 |
| 166 | + |
| 167 | + /// Link number out of range. |
| 168 | + case ELNRNG = 48 |
| 169 | + |
| 170 | + /// Protocol driver not attached. |
| 171 | + case EUNATCH = 49 |
| 172 | + |
| 173 | + /// No CSI structure available. |
| 174 | + case ENOCSI = 50 |
| 175 | + |
| 176 | + /// Level 2 halted. |
| 177 | + case EL2HLT = 51 |
| 178 | + case EBADE = 52 /* Invalid exchange */ |
| 179 | + case EBADR = 53 /* Invalid request descriptor */ |
| 180 | + case EXFULL = 54 /* Exchange full */ |
| 181 | + case ENOANO = 55 /* No anode */ |
| 182 | + case EBADRQC = 56 /* Invalid request code */ |
| 183 | + case EBADSLT = 57 /* Invalid slot */ |
| 184 | + |
| 185 | + public static var EDEADLOCK: POSIXErrorCode { return .EDEADLK } |
| 186 | + |
| 187 | + case EBFONT = 59 /* Bad font file format */ |
| 188 | + case ENOSTR = 60 /* Device not a stream */ |
| 189 | + case ENODATA = 61 /* No data available */ |
| 190 | + case ETIME = 62 /* Timer expired */ |
| 191 | + case ENOSR = 63 /* Out of streams resources */ |
| 192 | + case ENONET = 64 /* Machine is not on the network */ |
| 193 | + case ENOPKG = 65 /* Package not installed */ |
| 194 | + case EREMOTE = 66 /* Object is remote */ |
| 195 | + case ENOLINK = 67 /* Link has been severed */ |
| 196 | + case EADV = 68 /* Advertise error */ |
| 197 | + case ESRMNT = 69 /* Srmount error */ |
| 198 | + case ECOMM = 70 /* Communication error on send */ |
| 199 | + case EPROTO = 71 /* Protocol error */ |
| 200 | + case EMULTIHOP = 72 /* Multihop attempted */ |
| 201 | + case EDOTDOT = 73 /* RFS specific error */ |
| 202 | + case EBADMSG = 74 /* Not a data message */ |
| 203 | + case EOVERFLOW = 75 /* Value too large for defined data type */ |
| 204 | + case ENOTUNIQ = 76 /* Name not unique on network */ |
| 205 | + case EBADFD = 77 /* File descriptor in bad state */ |
| 206 | + case EREMCHG = 78 /* Remote address changed */ |
| 207 | + case ELIBACC = 79 /* Can not access a needed shared library */ |
| 208 | + case ELIBBAD = 80 /* Accessing a corrupted shared library */ |
| 209 | + case ELIBSCN = 81 /* .lib section in a.out corrupted */ |
| 210 | + case ELIBMAX = 82 /* Attempting to link in too many shared libraries */ |
| 211 | + case ELIBEXEC = 83 /* Cannot exec a shared library directly */ |
| 212 | + case EILSEQ = 84 /* Illegal byte sequence */ |
| 213 | + case ERESTART = 85 /* Interrupted system call should be restarted */ |
| 214 | + case ESTRPIPE = 86 /* Streams pipe error */ |
| 215 | + case EUSERS = 87 /* Too many users */ |
| 216 | + case ENOTSOCK = 88 /* Socket operation on non-socket */ |
| 217 | + case EDESTADDRREQ = 89 /* Destination address required */ |
| 218 | + case EMSGSIZE = 90 /* Message too long */ |
| 219 | + case EPROTOTYPE = 91 /* Protocol wrong type for socket */ |
| 220 | + case ENOPROTOOPT = 92 /* Protocol not available */ |
| 221 | + case EPROTONOSUPPORT = 93 /* Protocol not supported */ |
| 222 | + case ESOCKTNOSUPPORT = 94 /* Socket type not supported */ |
| 223 | + case EOPNOTSUPP = 95 /* Operation not supported on transport endpoint */ |
| 224 | + case EPFNOSUPPORT = 96 /* Protocol family not supported */ |
| 225 | + case EAFNOSUPPORT = 97 /* Address family not supported by protocol */ |
| 226 | + case EADDRINUSE = 98 /* Address already in use */ |
| 227 | + case EADDRNOTAVAIL = 99 /* Cannot assign requested address */ |
| 228 | + case ENETDOWN = 100 /* Network is down */ |
| 229 | + case ENETUNREACH = 101 /* Network is unreachable */ |
| 230 | + case ENETRESET = 102 /* Network dropped connection because of reset */ |
| 231 | + case ECONNABORTED = 103 /* Software caused connection abort */ |
| 232 | + case ECONNRESET = 104 /* Connection reset by peer */ |
| 233 | + case ENOBUFS = 105 /* No buffer space available */ |
| 234 | + case EISCONN = 106 /* Transport endpoint is already connected */ |
| 235 | + case ENOTCONN = 107 /* Transport endpoint is not connected */ |
| 236 | + case ESHUTDOWN = 108 /* Cannot send after transport endpoint shutdown */ |
| 237 | + case ETOOMANYREFS = 109 /* Too many references: cannot splice */ |
| 238 | + case ETIMEDOUT = 110 /* Connection timed out */ |
| 239 | + case ECONNREFUSED = 111 /* Connection refused */ |
| 240 | + case EHOSTDOWN = 112 /* Host is down */ |
| 241 | + case EHOSTUNREACH = 113 /* No route to host */ |
| 242 | + case EALREADY = 114 /* Operation already in progress */ |
| 243 | + case EINPROGRESS = 115 /* Operation now in progress */ |
| 244 | + case ESTALE = 116 /* Stale NFS file handle */ |
| 245 | + case EUCLEAN = 117 /* Structure needs cleaning */ |
| 246 | + case ENOTNAM = 118 /* Not a XENIX named type file */ |
| 247 | + case ENAVAIL = 119 /* No XENIX semaphores available */ |
| 248 | + case EISNAM = 120 /* Is a named type file */ |
| 249 | + case EREMOTEIO = 121 /* Remote I/O error */ |
| 250 | + case EDQUOT = 122 /* Quota exceeded */ |
| 251 | + |
| 252 | + case ENOMEDIUM = 123 /* No medium found */ |
| 253 | + case EMEDIUMTYPE = 124 /* Wrong medium type */ |
| 254 | + case ECANCELED = 125 /* Operation Canceled */ |
| 255 | + case ENOKEY = 126 /* Required key not available */ |
| 256 | + case EKEYEXPIRED = 127 /* Key has expired */ |
| 257 | + case EKEYREVOKED = 128 /* Key has been revoked */ |
| 258 | + case EKEYREJECTED = 129 /* Key was rejected by service */ |
| 259 | + |
| 260 | + /* for robust mutexes */ |
| 261 | + case EOWNERDEAD = 130 /* Owner died */ |
| 262 | + case ENOTRECOVERABLE = 131 /* State not recoverable */ |
43 | 263 | } |
44 | 264 |
|
45 | 265 | #endif |
|
0 commit comments