1 use core::{fmt, result};
2 
3 #[derive(Eq, PartialEq)]
4 pub struct Error {
5     pub errno: i32,
6 }
7 
8 pub type Result<T, E = Error> = result::Result<T, E>;
9 
10 impl Error {
new(errno: i32) -> Error11     pub fn new(errno: i32) -> Error {
12         Error { errno: errno }
13     }
14 
mux(result: Result<usize>) -> usize15     pub fn mux(result: Result<usize>) -> usize {
16         match result {
17             Ok(value) => value,
18             Err(error) => -error.errno as usize,
19         }
20     }
21 
demux(value: usize) -> Result<usize>22     pub fn demux(value: usize) -> Result<usize> {
23         let errno = -(value as i32);
24         if errno >= 1 && errno < STR_ERROR.len() as i32 {
25             Err(Error::new(errno))
26         } else {
27             Ok(value)
28         }
29     }
30 
text(&self) -> &'static str31     pub fn text(&self) -> &'static str {
32         STR_ERROR.get(self.errno as usize).map(|&x| x).unwrap_or("Unknown Error")
33     }
34 }
35 
36 impl fmt::Debug for Error {
fmt(&self, f: &mut fmt::Formatter) -> result::Result<(), fmt::Error>37     fn fmt(&self, f: &mut fmt::Formatter) -> result::Result<(), fmt::Error> {
38         f.write_str(self.text())
39     }
40 }
41 
42 impl fmt::Display for Error {
fmt(&self, f: &mut fmt::Formatter) -> result::Result<(), fmt::Error>43     fn fmt(&self, f: &mut fmt::Formatter) -> result::Result<(), fmt::Error> {
44         f.write_str(self.text())
45     }
46 }
47 
48 pub const EPERM: i32 = 1;  /* Operation not permitted */
49 pub const ENOENT: i32 = 2;  /* No such file or directory */
50 pub const ESRCH: i32 = 3;  /* No such process */
51 pub const EINTR: i32 = 4;  /* Interrupted system call */
52 pub const EIO: i32 = 5;  /* I/O error */
53 pub const ENXIO: i32 = 6;  /* No such device or address */
54 pub const E2BIG: i32 = 7;  /* Argument list too long */
55 pub const ENOEXEC: i32 = 8;  /* Exec format error */
56 pub const EBADF: i32 = 9;  /* Bad file number */
57 pub const ECHILD: i32 = 10;  /* No child processes */
58 pub const EAGAIN: i32 = 11;  /* Try again */
59 pub const ENOMEM: i32 = 12;  /* Out of memory */
60 pub const EACCES: i32 = 13;  /* Permission denied */
61 pub const EFAULT: i32 = 14;  /* Bad address */
62 pub const ENOTBLK: i32 = 15;  /* Block device required */
63 pub const EBUSY: i32 = 16;  /* Device or resource busy */
64 pub const EEXIST: i32 = 17;  /* File exists */
65 pub const EXDEV: i32 = 18;  /* Cross-device link */
66 pub const ENODEV: i32 = 19;  /* No such device */
67 pub const ENOTDIR: i32 = 20;  /* Not a directory */
68 pub const EISDIR: i32 = 21;  /* Is a directory */
69 pub const EINVAL: i32 = 22;  /* Invalid argument */
70 pub const ENFILE: i32 = 23;  /* File table overflow */
71 pub const EMFILE: i32 = 24;  /* Too many open files */
72 pub const ENOTTY: i32 = 25;  /* Not a typewriter */
73 pub const ETXTBSY: i32 = 26;  /* Text file busy */
74 pub const EFBIG: i32 = 27;  /* File too large */
75 pub const ENOSPC: i32 = 28;  /* No space left on device */
76 pub const ESPIPE: i32 = 29;  /* Illegal seek */
77 pub const EROFS: i32 = 30;  /* Read-only file system */
78 pub const EMLINK: i32 = 31;  /* Too many links */
79 pub const EPIPE: i32 = 32;  /* Broken pipe */
80 pub const EDOM: i32 = 33;  /* Math argument out of domain of func */
81 pub const ERANGE: i32 = 34;  /* Math result not representable */
82 pub const EDEADLK: i32 = 35;  /* Resource deadlock would occur */
83 pub const ENAMETOOLONG: i32 = 36;  /* File name too long */
84 pub const ENOLCK: i32 = 37;  /* No record locks available */
85 pub const ENOSYS: i32 = 38;  /* Function not implemented */
86 pub const ENOTEMPTY: i32 = 39;  /* Directory not empty */
87 pub const ELOOP: i32 = 40;  /* Too many symbolic links encountered */
88 pub const EWOULDBLOCK: i32 = 41;  /* Operation would block */
89 pub const ENOMSG: i32 = 42;  /* No message of desired type */
90 pub const EIDRM: i32 = 43;  /* Identifier removed */
91 pub const ECHRNG: i32 = 44;  /* Channel number out of range */
92 pub const EL2NSYNC: i32 = 45;  /* Level 2 not synchronized */
93 pub const EL3HLT: i32 = 46;  /* Level 3 halted */
94 pub const EL3RST: i32 = 47;  /* Level 3 reset */
95 pub const ELNRNG: i32 = 48;  /* Link number out of range */
96 pub const EUNATCH: i32 = 49;  /* Protocol driver not attached */
97 pub const ENOCSI: i32 = 50;  /* No CSI structure available */
98 pub const EL2HLT: i32 = 51;  /* Level 2 halted */
99 pub const EBADE: i32 = 52;  /* Invalid exchange */
100 pub const EBADR: i32 = 53;  /* Invalid request descriptor */
101 pub const EXFULL: i32 = 54;  /* Exchange full */
102 pub const ENOANO: i32 = 55;  /* No anode */
103 pub const EBADRQC: i32 = 56;  /* Invalid request code */
104 pub const EBADSLT: i32 = 57;  /* Invalid slot */
105 pub const EDEADLOCK: i32 = 58; /* Resource deadlock would occur */
106 pub const EBFONT: i32 = 59;  /* Bad font file format */
107 pub const ENOSTR: i32 = 60;  /* Device not a stream */
108 pub const ENODATA: i32 = 61;  /* No data available */
109 pub const ETIME: i32 = 62;  /* Timer expired */
110 pub const ENOSR: i32 = 63;  /* Out of streams resources */
111 pub const ENONET: i32 = 64;  /* Machine is not on the network */
112 pub const ENOPKG: i32 = 65;  /* Package not installed */
113 pub const EREMOTE: i32 = 66;  /* Object is remote */
114 pub const ENOLINK: i32 = 67;  /* Link has been severed */
115 pub const EADV: i32 = 68;  /* Advertise error */
116 pub const ESRMNT: i32 = 69;  /* Srmount error */
117 pub const ECOMM: i32 = 70;  /* Communication error on send */
118 pub const EPROTO: i32 = 71;  /* Protocol error */
119 pub const EMULTIHOP: i32 = 72;  /* Multihop attempted */
120 pub const EDOTDOT: i32 = 73;  /* RFS specific error */
121 pub const EBADMSG: i32 = 74;  /* Not a data message */
122 pub const EOVERFLOW: i32 = 75;  /* Value too large for defined data type */
123 pub const ENOTUNIQ: i32 = 76;  /* Name not unique on network */
124 pub const EBADFD: i32 = 77;  /* File descriptor in bad state */
125 pub const EREMCHG: i32 = 78;  /* Remote address changed */
126 pub const ELIBACC: i32 = 79;  /* Can not access a needed shared library */
127 pub const ELIBBAD: i32 = 80;  /* Accessing a corrupted shared library */
128 pub const ELIBSCN: i32 = 81;  /* .lib section in a.out corrupted */
129 pub const ELIBMAX: i32 = 82;  /* Attempting to link in too many shared libraries */
130 pub const ELIBEXEC: i32 = 83;  /* Cannot exec a shared library directly */
131 pub const EILSEQ: i32 = 84;  /* Illegal byte sequence */
132 pub const ERESTART: i32 = 85;  /* Interrupted system call should be restarted */
133 pub const ESTRPIPE: i32 = 86;  /* Streams pipe error */
134 pub const EUSERS: i32 = 87;  /* Too many users */
135 pub const ENOTSOCK: i32 = 88;  /* Socket operation on non-socket */
136 pub const EDESTADDRREQ: i32 = 89;  /* Destination address required */
137 pub const EMSGSIZE: i32 = 90;  /* Message too long */
138 pub const EPROTOTYPE: i32 = 91;  /* Protocol wrong type for socket */
139 pub const ENOPROTOOPT: i32 = 92;  /* Protocol not available */
140 pub const EPROTONOSUPPORT: i32 = 93;  /* Protocol not supported */
141 pub const ESOCKTNOSUPPORT: i32 = 94;  /* Socket type not supported */
142 pub const EOPNOTSUPP: i32 = 95;  /* Operation not supported on transport endpoint */
143 pub const EPFNOSUPPORT: i32 = 96;  /* Protocol family not supported */
144 pub const EAFNOSUPPORT: i32 = 97;  /* Address family not supported by protocol */
145 pub const EADDRINUSE: i32 = 98;  /* Address already in use */
146 pub const EADDRNOTAVAIL: i32 = 99;  /* Cannot assign requested address */
147 pub const ENETDOWN: i32 = 100; /* Network is down */
148 pub const ENETUNREACH: i32 = 101; /* Network is unreachable */
149 pub const ENETRESET: i32 = 102; /* Network dropped connection because of reset */
150 pub const ECONNABORTED: i32 = 103; /* Software caused connection abort */
151 pub const ECONNRESET: i32 = 104; /* Connection reset by peer */
152 pub const ENOBUFS: i32 = 105; /* No buffer space available */
153 pub const EISCONN: i32 = 106; /* Transport endpoint is already connected */
154 pub const ENOTCONN: i32 = 107; /* Transport endpoint is not connected */
155 pub const ESHUTDOWN: i32 = 108; /* Cannot send after transport endpoint shutdown */
156 pub const ETOOMANYREFS: i32 = 109; /* Too many references: cannot splice */
157 pub const ETIMEDOUT: i32 = 110; /* Connection timed out */
158 pub const ECONNREFUSED: i32 = 111; /* Connection refused */
159 pub const EHOSTDOWN: i32 = 112; /* Host is down */
160 pub const EHOSTUNREACH: i32 = 113; /* No route to host */
161 pub const EALREADY: i32 = 114; /* Operation already in progress */
162 pub const EINPROGRESS: i32 = 115; /* Operation now in progress */
163 pub const ESTALE: i32 = 116; /* Stale NFS file handle */
164 pub const EUCLEAN: i32 = 117; /* Structure needs cleaning */
165 pub const ENOTNAM: i32 = 118; /* Not a XENIX named type file */
166 pub const ENAVAIL: i32 = 119; /* No XENIX semaphores available */
167 pub const EISNAM: i32 = 120; /* Is a named type file */
168 pub const EREMOTEIO: i32 = 121; /* Remote I/O error */
169 pub const EDQUOT: i32 = 122; /* Quota exceeded */
170 pub const ENOMEDIUM: i32 = 123; /* No medium found */
171 pub const EMEDIUMTYPE: i32 = 124; /* Wrong medium type */
172 pub const ECANCELED: i32 = 125; /* Operation Canceled */
173 pub const ENOKEY: i32 = 126; /* Required key not available */
174 pub const EKEYEXPIRED: i32 = 127; /* Key has expired */
175 pub const EKEYREVOKED: i32 = 128; /* Key has been revoked */
176 pub const EKEYREJECTED: i32 = 129; /* Key was rejected by service */
177 pub const EOWNERDEAD: i32 = 130; /* Owner died */
178 pub const ENOTRECOVERABLE: i32 = 131; /* State not recoverable */
179 
180 pub static STR_ERROR: [&'static str; 132] = ["Success",
181                                              "Operation not permitted",
182                                              "No such file or directory",
183                                              "No such process",
184                                              "Interrupted system call",
185                                              "I/O error",
186                                              "No such device or address",
187                                              "Argument list too long",
188                                              "Exec format error",
189                                              "Bad file number",
190                                              "No child processes",
191                                              "Try again",
192                                              "Out of memory",
193                                              "Permission denied",
194                                              "Bad address",
195                                              "Block device required",
196                                              "Device or resource busy",
197                                              "File exists",
198                                              "Cross-device link",
199                                              "No such device",
200                                              "Not a directory",
201                                              "Is a directory",
202                                              "Invalid argument",
203                                              "File table overflow",
204                                              "Too many open files",
205                                              "Not a typewriter",
206                                              "Text file busy",
207                                              "File too large",
208                                              "No space left on device",
209                                              "Illegal seek",
210                                              "Read-only file system",
211                                              "Too many links",
212                                              "Broken pipe",
213                                              "Math argument out of domain of func",
214                                              "Math result not representable",
215                                              "Resource deadlock would occur",
216                                              "File name too long",
217                                              "No record locks available",
218                                              "Function not implemented",
219                                              "Directory not empty",
220                                              "Too many symbolic links encountered",
221                                              "Operation would block",
222                                              "No message of desired type",
223                                              "Identifier removed",
224                                              "Channel number out of range",
225                                              "Level 2 not synchronized",
226                                              "Level 3 halted",
227                                              "Level 3 reset",
228                                              "Link number out of range",
229                                              "Protocol driver not attached",
230                                              "No CSI structure available",
231                                              "Level 2 halted",
232                                              "Invalid exchange",
233                                              "Invalid request descriptor",
234                                              "Exchange full",
235                                              "No anode",
236                                              "Invalid request code",
237                                              "Invalid slot",
238                                              "Resource deadlock would occur",
239                                              "Bad font file format",
240                                              "Device not a stream",
241                                              "No data available",
242                                              "Timer expired",
243                                              "Out of streams resources",
244                                              "Machine is not on the network",
245                                              "Package not installed",
246                                              "Object is remote",
247                                              "Link has been severed",
248                                              "Advertise error",
249                                              "Srmount error",
250                                              "Communication error on send",
251                                              "Protocol error",
252                                              "Multihop attempted",
253                                              "RFS specific error",
254                                              "Not a data message",
255                                              "Value too large for defined data type",
256                                              "Name not unique on network",
257                                              "File descriptor in bad state",
258                                              "Remote address changed",
259                                              "Can not access a needed shared library",
260                                              "Accessing a corrupted shared library",
261                                              ".lib section in a.out corrupted",
262                                              "Attempting to link in too many shared libraries",
263                                              "Cannot exec a shared library directly",
264                                              "Illegal byte sequence",
265                                              "Interrupted system call should be restarted",
266                                              "Streams pipe error",
267                                              "Too many users",
268                                              "Socket operation on non-socket",
269                                              "Destination address required",
270                                              "Message too long",
271                                              "Protocol wrong type for socket",
272                                              "Protocol not available",
273                                              "Protocol not supported",
274                                              "Socket type not supported",
275                                              "Operation not supported on transport endpoint",
276                                              "Protocol family not supported",
277                                              "Address family not supported by protocol",
278                                              "Address already in use",
279                                              "Cannot assign requested address",
280                                              "Network is down",
281                                              "Network is unreachable",
282                                              "Network dropped connection because of reset",
283                                              "Software caused connection abort",
284                                              "Connection reset by peer",
285                                              "No buffer space available",
286                                              "Transport endpoint is already connected",
287                                              "Transport endpoint is not connected",
288                                              "Cannot send after transport endpoint shutdown",
289                                              "Too many references: cannot splice",
290                                              "Connection timed out",
291                                              "Connection refused",
292                                              "Host is down",
293                                              "No route to host",
294                                              "Operation already in progress",
295                                              "Operation now in progress",
296                                              "Stale NFS file handle",
297                                              "Structure needs cleaning",
298                                              "Not a XENIX named type file",
299                                              "No XENIX semaphores available",
300                                              "Is a named type file",
301                                              "Remote I/O error",
302                                              "Quota exceeded",
303                                              "No medium found",
304                                              "Wrong medium type",
305                                              "Operation Canceled",
306                                              "Required key not available",
307                                              "Key has expired",
308                                              "Key has been revoked",
309                                              "Key was rejected by service",
310                                              "Owner died",
311                                              "State not recoverable"];
312