1 use num_traits::{FromPrimitive, ToPrimitive};
2 
3 #[repr(i32)]
4 #[derive(Debug, FromPrimitive, ToPrimitive, PartialEq, Eq, Clone)]
5 #[allow(dead_code, non_camel_case_types)]
6 pub enum SystemError {
7     /// 操作不被允许 Operation not permitted.
8     EPERM = 1,
9     /// 没有指定的文件或目录 No such file or directory.
10     ENOENT = 2,
11     /// 没有这样的进程 No such process.
12     ESRCH = 3,
13     /// 被中断的函数 Interrupted function.
14     EINTR = 4,
15     /// I/O错误 I/O error.
16     EIO = 5,
17     /// 没有这样的设备或地址 No such device or address.
18     ENXIO = 6,
19     /// 参数列表过长,或者在输出buffer中缺少空间 或者参数比系统内建的最大值要大 Argument list too long.
20     E2BIG = 7,
21     /// 可执行文件格式错误 Executable file format error
22     ENOEXEC = 8,
23     /// 错误的文件描述符 Bad file descriptor.
24     EBADF = 9,
25     /// 没有子进程 No child processes.
26     ECHILD = 10,
27     /// 资源不可用,请重试。 Resource unavailable, try again.(may be the same value as [EWOULDBLOCK])
28     ///
29     /// 操作将被禁止 Operation would block.(may be the same value as [EAGAIN]).
30     EAGAIN_OR_EWOULDBLOCK = 11,
31     /// 没有足够的空间 Not enough space.
32     ENOMEM = 12,
33     /// 访问被拒绝 Permission denied
34     EACCES = 13,
35     /// 错误的地址 Bad address
36     EFAULT = 14,
37     /// 需要块设备 Block device required
38     ENOTBLK = 15,
39     /// 设备或资源忙 Device or resource busy.
40     EBUSY = 16,
41     /// 文件已存在 File exists.
42     EEXIST = 17,
43     /// 跨设备连接 Cross-device link.
44     EXDEV = 18,
45     /// 没有指定的设备 No such device.
46     ENODEV = 19,
47     /// 不是目录 Not a directory.
48     ENOTDIR = 20,
49     /// 是一个目录 Is a directory
50     EISDIR = 21,
51     /// 不可用的参数 Invalid argument.
52     EINVAL = 22,
53     /// 系统中打开的文件过多 Too many files open in system.
54     ENFILE = 23,
55     /// 文件描述符的值过大 File descriptor value too large.
56     EMFILE = 24,
57     /// 不正确的I/O控制操作 Inappropriate I/O control operation.
58     ENOTTY = 25,
59     /// 文本文件忙 Text file busy.
60     ETXTBSY = 26,
61     /// 文件太大 File too large.
62     EFBIG = 27,
63     /// 设备上没有空间 No space left on device.
64     ENOSPC = 28,
65     /// 错误的寻道.当前文件是pipe,不允许seek请求  Invalid seek.
66     ESPIPE = 29,
67     /// 只读的文件系统 Read-only file system.
68     EROFS = 30,
69     /// 链接数过多 Too many links.
70     EMLINK = 31,
71     /// 断开的管道 Broken pipe.
72     EPIPE = 32,
73     /// 数学参数超出作用域 Mathematics argument out of domain of function.
74     EDOM = 33,
75     /// 结果过大 Result too large.
76     ERANGE = 34,
77     /// 资源死锁将要发生 Resource deadlock would occur.
78     EDEADLK = 35,
79     /// 文件名过长 Filename too long.
80     ENAMETOOLONG = 36,
81     /// 没有可用的锁 No locks available.
82     ENOLCK = 37,
83     /// 功能不支持 Function not supported.
84     ENOSYS = 38,
85     /// 目录非空 Directory not empty.
86     ENOTEMPTY = 39,
87     /// 符号链接级别过多 Too many levels of symbolic links.
88     ELOOP = 40,
89     /// 没有期待类型的消息 No message of the desired type.
90     ENOMSG = 41,
91     /// 标志符被移除 Identifier removed.
92     EIDRM = 42,
93     /// 通道号超出范围 Channel number out of range
94     ECHRNG = 43,
95     /// 二级不同步 Level 2 not synchronized
96     EL2NSYNC = 44,
97     /// 三级暂停 Level 3 halted
98     EL3HLT = 45,
99     /// 三级重置 Level 3 reset
100     EL3RST = 46,
101     /// 链接号超出范围 Link number out of range
102     ELNRNG = 47,
103     /// 未连接协议驱动程序 Protocol driver not attached
104     EUNATCH = 48,
105     /// 没有可用的CSI结构 No CSI structure available
106     ENOCSI = 49,
107     /// 二级暂停 Level 2 halted
108     EL2HLT = 50,
109     /// 无效交换 Invalid exchange
110     EBADE = 51,
111     /// 无效的请求描述符 Invalid request descriptor
112     EBADR = 52,
113     /// 交换满 Exchange full
114     EXFULL = 53,
115     /// 无阳极 No anode
116     ENOANO = 54,
117     /// 请求码无效 Invalid request code
118     EBADRQC = 55,
119     /// 无效插槽 Invalid slot
120     EBADSLT = 56,
121     /// 资源死锁 Resource deadlock would occur
122     EDEADLOCK = 57,
123     /// 错误的字体文件格式 Bad font file format
124     EBFONT = 58,
125     /// 不是STREAM Not a STREAM
126     ENOSTR = 59,
127     /// 队列头没有可读取的消息 No message is available on the STREAM head read queue.
128     ENODATA = 60,
129     /// 流式ioctl()超时 Stream ioctl() timeout
130     ETIME = 61,
131     /// 没有STREAM资源  No STREAM resources.
132     ENOSR = 62,
133     /// 机器不在网络上 Machine is not on the network
134     ENONET = 63,
135     /// 未安装软件包 Package not installed
136     ENOPKG = 64,
137     /// 远程对象 Object is remote
138     EREMOTE = 65,
139     /// 保留 Reserved.
140     ENOLINK = 66,
141     /// 外设错误 Advertise error.
142     EADV = 67,
143     /// 安装错误 Srmount error
144     ESRMNT = 68,
145     /// 发送时发生通信错误 Communication error on send
146     ECOMM = 69,
147     /// 协议错误 Protocol error.
148     EPROTO = 70,
149     /// 保留使用 Reserved.
150     EMULTIHOP = 71,
151     /// RFS特定错误 RFS specific error
152     EDOTDOT = 72,
153     /// 错误的消息 Bad message.
154     EBADMSG = 73,
155     /// 数值过大,产生溢出 Value too large to be stored in data type.
156     EOVERFLOW = 74,
157     /// 名称在网络上不是唯一的 Name not unique on network
158     ENOTUNIQ = 75,
159     /// 处于不良状态的文件描述符 File descriptor in bad state
160     EBADFD = 76,
161     /// 远程地址已更改 Remote address changed
162     EREMCHG = 77,
163     /// 无法访问所需的共享库 Can not access a needed shared library
164     ELIBACC = 78,
165     /// 访问损坏的共享库 Accessing a corrupted shared library
166     ELIBBAD = 79,
167     /// a. out中的.lib部分已损坏 .lib section in a.out corrupted
168     ELIBSCN = 80,
169     /// 尝试链接太多共享库 Attempting to link in too many shared libraries
170     ELIBMAX = 81,
171     /// 无法直接执行共享库 Cannot exec a shared library directly
172     ELIBEXEC = 82,
173     /// 不合法的字符序列 Illegal byte sequence.
174     EILSEQ = 83,
175     /// 中断的系统调用应该重新启动 Interrupted system call should be restarted
176     ERESTART = 84,
177     /// 流管道错误 Streams pipe error
178     ESTRPIPE = 85,
179     /// 用户太多 Too many users
180     EUSERS = 86,
181     /// 不是一个套接字 Not a socket.
182     ENOTSOCK = 87,
183     /// 需要目标地址 Destination address required.
184     EDESTADDRREQ = 88,
185     /// 消息过大 Message too large.
186     EMSGSIZE = 89,
187     /// 对于套接字而言,错误的协议 Protocol wrong type for socket.
188     EPROTOTYPE = 90,
189     /// 协议不可用 Protocol not available.
190     ENOPROTOOPT = 91,
191     /// 协议不被支持 Protocol not supported.
192     EPROTONOSUPPORT = 92,
193     /// 不支持套接字类型 Socket type not supported
194     ESOCKTNOSUPPORT = 93,
195     /// 套接字不支持该操作 Operation not supported on socket (may be the same value as [ENOTSUP]).
196     ///
197     /// 不被支持 Not supported (may be the same value as [EOPNOTSUPP]).
198     EOPNOTSUPP_OR_ENOTSUP = 94,
199     /// 不支持协议系列 Protocol family not supported
200     EPFNOSUPPORT = 95,
201     /// 地址family不支持 Address family not supported.
202     EAFNOSUPPORT = 96,
203     /// 地址正在被使用 Address in use.
204     EADDRINUSE = 97,
205     /// 地址不可用 Address  not available.
206     EADDRNOTAVAIL = 98,
207     /// 网络已关闭 Network is down.
208     ENETDOWN = 99,
209     /// 网络不可达 Network unreachable.
210     ENETUNREACH = 100,
211     /// 网络连接已断开 Connection aborted by network.
212     ENETRESET = 101,
213     /// 连接已断开 Connection aborted.
214     ECONNABORTED = 102,
215     /// 连接被重置 Connection reset.
216     ECONNRESET = 103,
217     /// 缓冲区空间不足 No buffer space available.
218     ENOBUFS = 104,
219     /// 套接字已连接 Socket is connected.
220     EISCONN = 105,
221     /// 套接字未连接 The socket is not connected.
222     ENOTCONN = 106,
223     /// 传输端点关闭后无法发送 Cannot send after transport endpoint shutdown
224     ESHUTDOWN = 107,
225     /// 引用太多:无法拼接 Too many references: cannot splice
226     ETOOMANYREFS = 108,
227     /// 连接超时 Connection timed out.
228     ETIMEDOUT = 109,
229     /// 连接被拒绝 Connection refused.
230     ECONNREFUSED = 110,
231     /// 主机已关闭 Host is down
232     EHOSTDOWN = 111,
233     /// 主机不可达 Host is unreachable.
234     EHOSTUNREACH = 112,
235     /// 连接已经在处理 Connection already in progress.
236     EALREADY = 113,
237     /// 操作正在处理 Operation in progress.
238     EINPROGRESS = 114,
239     /// 保留 Reserved.
240     ESTALE = 115,
241     /// 结构需要清理 Structure needs cleaning
242     EUCLEAN = 116,
243     /// 不是XENIX命名类型文件 Not a XENIX named type file
244     ENOTNAM = 117,
245     /// 没有可用的XENIX信号量 No XENIX semaphores available
246     ENAVAIL = 118,
247     /// 是命名类型文件 Is a named type file
248     EISNAM = 119,
249     /// 远程I/O错误 Remote I/O error
250     EREMOTEIO = 120,
251     /// 保留使用 Reserved
252     EDQUOT = 121,
253     /// 没有找到媒介 No medium found
254     ENOMEDIUM = 122,
255     /// 介质类型错误 Wrong medium type
256     EMEDIUMTYPE = 123,
257     /// 操作被取消 Operation canceled.
258     ECANCELED = 124,
259     /// 所需的密钥不可用 Required key not available
260     ENOKEY = 125,
261     /// 密钥已过期 Key has expired
262     EKEYEXPIRED = 126,
263     /// 密钥已被撤销 Key has been revoked
264     EKEYREVOKED = 127,
265     /// 密钥被服务拒绝 Key has been revoked
266     EKEYREJECTED = 128,
267     /// 之前的拥有者挂了 Previous owner died.
268     EOWNERDEAD = 129,
269     /// 状态不可恢复 State not recoverable.
270     ENOTRECOVERABLE = 130,
271 }
272 
273 impl SystemError {
274     /// @brief 把posix错误码转换为系统错误枚举类型。
from_posix_errno(errno: i32) -> Option<SystemError>275     pub fn from_posix_errno(errno: i32) -> Option<SystemError> {
276         // posix 错误码是小于0的
277         if errno >= 0 {
278             return None;
279         }
280         return <Self as FromPrimitive>::from_i32(-errno);
281     }
282 
283     /// @brief 把系统错误枚举类型转换为负数posix错误码。
to_posix_errno(&self) -> i32284     pub fn to_posix_errno(&self) -> i32 {
285         return -<Self as ToPrimitive>::to_i32(self).unwrap();
286     }
287 }
288