Line | Count | Source (jump to first uncovered line) |
1 | | /* GIO - GLib Input, Output and Streaming Library |
2 | | * |
3 | | * Copyright (C) 2006-2007 Red Hat, Inc. |
4 | | * |
5 | | * This library is free software; you can redistribute it and/or |
6 | | * modify it under the terms of the GNU Lesser General Public |
7 | | * License as published by the Free Software Foundation; either |
8 | | * version 2.1 of the License, or (at your option) any later version. |
9 | | * |
10 | | * This library is distributed in the hope that it will be useful, |
11 | | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
12 | | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
13 | | * Lesser General Public License for more details. |
14 | | * |
15 | | * You should have received a copy of the GNU Lesser General |
16 | | * Public License along with this library; if not, see <http://www.gnu.org/licenses/>. |
17 | | * |
18 | | * Author: Alexander Larsson <alexl@redhat.com> |
19 | | */ |
20 | | |
21 | | #include "config.h" |
22 | | #include <errno.h> |
23 | | #include "gioerror.h" |
24 | | |
25 | | #ifdef G_OS_WIN32 |
26 | | #include <winsock2.h> |
27 | | #endif |
28 | | |
29 | | /** |
30 | | * SECTION:gioerror |
31 | | * @short_description: Error helper functions |
32 | | * @include: gio/gio.h |
33 | | * |
34 | | * Contains helper functions for reporting errors to the user. |
35 | | **/ |
36 | | |
37 | | /** |
38 | | * g_io_error_quark: |
39 | | * |
40 | | * Gets the GIO Error Quark. |
41 | | * |
42 | | * Returns: a #GQuark. |
43 | | **/ |
44 | | G_DEFINE_QUARK (g-io-error-quark, g_io_error) |
45 | | |
46 | | /** |
47 | | * g_io_error_from_errno: |
48 | | * @err_no: Error number as defined in errno.h. |
49 | | * |
50 | | * Converts errno.h error codes into GIO error codes. The fallback |
51 | | * value %G_IO_ERROR_FAILED is returned for error codes not currently |
52 | | * handled (but note that future GLib releases may return a more |
53 | | * specific value instead). |
54 | | * |
55 | | * As %errno is global and may be modified by intermediate function |
56 | | * calls, you should save its value as soon as the call which sets it |
57 | | * returns: |
58 | | * |[ |
59 | | * int saved_errno; |
60 | | * |
61 | | * ret = read (blah); |
62 | | * saved_errno = errno; |
63 | | * |
64 | | * g_io_error_from_errno (saved_errno); |
65 | | * ]| |
66 | | * |
67 | | * Returns: #GIOErrorEnum value for the given errno.h error number. |
68 | | **/ |
69 | | GIOErrorEnum |
70 | | g_io_error_from_errno (gint err_no) |
71 | 0 | { |
72 | 0 | switch (err_no) |
73 | 0 | { |
74 | 0 | #ifdef EEXIST |
75 | 0 | case EEXIST: |
76 | 0 | return G_IO_ERROR_EXISTS; |
77 | 0 | break; |
78 | 0 | #endif |
79 | | |
80 | 0 | #ifdef EISDIR |
81 | 0 | case EISDIR: |
82 | 0 | return G_IO_ERROR_IS_DIRECTORY; |
83 | 0 | break; |
84 | 0 | #endif |
85 | | |
86 | 0 | #ifdef EACCES |
87 | 0 | case EACCES: |
88 | 0 | return G_IO_ERROR_PERMISSION_DENIED; |
89 | 0 | break; |
90 | 0 | #endif |
91 | | |
92 | 0 | #ifdef ENAMETOOLONG |
93 | 0 | case ENAMETOOLONG: |
94 | 0 | return G_IO_ERROR_FILENAME_TOO_LONG; |
95 | 0 | break; |
96 | 0 | #endif |
97 | | |
98 | 0 | #ifdef ENOENT |
99 | 0 | case ENOENT: |
100 | 0 | return G_IO_ERROR_NOT_FOUND; |
101 | 0 | break; |
102 | 0 | #endif |
103 | | |
104 | 0 | #ifdef ENOTDIR |
105 | 0 | case ENOTDIR: |
106 | 0 | return G_IO_ERROR_NOT_DIRECTORY; |
107 | 0 | break; |
108 | 0 | #endif |
109 | | |
110 | 0 | #ifdef ENXIO |
111 | 0 | case ENXIO: |
112 | 0 | return G_IO_ERROR_NOT_REGULAR_FILE; |
113 | 0 | break; |
114 | 0 | #endif |
115 | | |
116 | 0 | #ifdef EROFS |
117 | 0 | case EROFS: |
118 | 0 | return G_IO_ERROR_READ_ONLY; |
119 | 0 | break; |
120 | 0 | #endif |
121 | | |
122 | 0 | #ifdef ELOOP |
123 | 0 | case ELOOP: |
124 | 0 | return G_IO_ERROR_TOO_MANY_LINKS; |
125 | 0 | break; |
126 | 0 | #endif |
127 | | |
128 | 0 | #ifdef ENOSPC |
129 | 0 | case ENOSPC: |
130 | 0 | return G_IO_ERROR_NO_SPACE; |
131 | 0 | break; |
132 | 0 | #endif |
133 | | |
134 | 0 | #ifdef ENOMEM |
135 | 0 | case ENOMEM: |
136 | 0 | return G_IO_ERROR_NO_SPACE; |
137 | 0 | break; |
138 | 0 | #endif |
139 | | |
140 | 0 | #ifdef EINVAL |
141 | 0 | case EINVAL: |
142 | 0 | return G_IO_ERROR_INVALID_ARGUMENT; |
143 | 0 | break; |
144 | 0 | #endif |
145 | | |
146 | 0 | #ifdef EPERM |
147 | 0 | case EPERM: |
148 | 0 | return G_IO_ERROR_PERMISSION_DENIED; |
149 | 0 | break; |
150 | 0 | #endif |
151 | | |
152 | 0 | #ifdef ECANCELED |
153 | 0 | case ECANCELED: |
154 | 0 | return G_IO_ERROR_CANCELLED; |
155 | 0 | break; |
156 | 0 | #endif |
157 | | |
158 | | /* ENOTEMPTY == EEXIST on AIX for backward compatibility reasons */ |
159 | 0 | #if defined (ENOTEMPTY) && (!defined (EEXIST) || (ENOTEMPTY != EEXIST)) |
160 | 0 | case ENOTEMPTY: |
161 | 0 | return G_IO_ERROR_NOT_EMPTY; |
162 | 0 | break; |
163 | 0 | #endif |
164 | | |
165 | 0 | #ifdef ENOTSUP |
166 | 0 | case ENOTSUP: |
167 | 0 | return G_IO_ERROR_NOT_SUPPORTED; |
168 | 0 | break; |
169 | 0 | #endif |
170 | | |
171 | | /* EOPNOTSUPP == ENOTSUP on Linux, but POSIX considers them distinct */ |
172 | | #if defined (EOPNOTSUPP) && (!defined (ENOTSUP) || (EOPNOTSUPP != ENOTSUP)) |
173 | | case EOPNOTSUPP: |
174 | | return G_IO_ERROR_NOT_SUPPORTED; |
175 | | break; |
176 | | #endif |
177 | | |
178 | 0 | #ifdef EPROTONOSUPPORT |
179 | 0 | case EPROTONOSUPPORT: |
180 | 0 | return G_IO_ERROR_NOT_SUPPORTED; |
181 | 0 | break; |
182 | 0 | #endif |
183 | | |
184 | 0 | #ifdef ESOCKTNOSUPPORT |
185 | 0 | case ESOCKTNOSUPPORT: |
186 | 0 | return G_IO_ERROR_NOT_SUPPORTED; |
187 | 0 | break; |
188 | 0 | #endif |
189 | | |
190 | 0 | #ifdef EPFNOSUPPORT |
191 | 0 | case EPFNOSUPPORT: |
192 | 0 | return G_IO_ERROR_NOT_SUPPORTED; |
193 | 0 | break; |
194 | 0 | #endif |
195 | | |
196 | 0 | #ifdef EAFNOSUPPORT |
197 | 0 | case EAFNOSUPPORT: |
198 | 0 | return G_IO_ERROR_NOT_SUPPORTED; |
199 | 0 | break; |
200 | 0 | #endif |
201 | | |
202 | 0 | #ifdef ETIMEDOUT |
203 | 0 | case ETIMEDOUT: |
204 | 0 | return G_IO_ERROR_TIMED_OUT; |
205 | 0 | break; |
206 | 0 | #endif |
207 | | |
208 | 0 | #ifdef EBUSY |
209 | 0 | case EBUSY: |
210 | 0 | return G_IO_ERROR_BUSY; |
211 | 0 | break; |
212 | 0 | #endif |
213 | | |
214 | 0 | #ifdef EWOULDBLOCK |
215 | 0 | case EWOULDBLOCK: |
216 | 0 | return G_IO_ERROR_WOULD_BLOCK; |
217 | 0 | break; |
218 | 0 | #endif |
219 | | |
220 | | /* EWOULDBLOCK == EAGAIN on most systems, but POSIX considers them distinct */ |
221 | | #if defined (EAGAIN) && (!defined (EWOULDBLOCK) || (EWOULDBLOCK != EAGAIN)) |
222 | | case EAGAIN: |
223 | | return G_IO_ERROR_WOULD_BLOCK; |
224 | | break; |
225 | | #endif |
226 | | |
227 | 0 | #ifdef EMFILE |
228 | 0 | case EMFILE: |
229 | 0 | return G_IO_ERROR_TOO_MANY_OPEN_FILES; |
230 | 0 | break; |
231 | 0 | #endif |
232 | | |
233 | 0 | #ifdef EADDRINUSE |
234 | 0 | case EADDRINUSE: |
235 | 0 | return G_IO_ERROR_ADDRESS_IN_USE; |
236 | 0 | break; |
237 | 0 | #endif |
238 | | |
239 | 0 | #ifdef EHOSTUNREACH |
240 | 0 | case EHOSTUNREACH: |
241 | 0 | return G_IO_ERROR_HOST_UNREACHABLE; |
242 | 0 | break; |
243 | 0 | #endif |
244 | | |
245 | 0 | #ifdef ENETUNREACH |
246 | 0 | case ENETUNREACH: |
247 | 0 | return G_IO_ERROR_NETWORK_UNREACHABLE; |
248 | 0 | break; |
249 | 0 | #endif |
250 | | |
251 | 0 | #ifdef ECONNREFUSED |
252 | 0 | case ECONNREFUSED: |
253 | 0 | return G_IO_ERROR_CONNECTION_REFUSED; |
254 | 0 | break; |
255 | 0 | #endif |
256 | | |
257 | 0 | #ifdef EPIPE |
258 | 0 | case EPIPE: |
259 | 0 | return G_IO_ERROR_BROKEN_PIPE; |
260 | 0 | break; |
261 | 0 | #endif |
262 | | |
263 | 0 | #ifdef ECONNRESET |
264 | 0 | case ECONNRESET: |
265 | 0 | return G_IO_ERROR_CONNECTION_CLOSED; |
266 | 0 | break; |
267 | 0 | #endif |
268 | | |
269 | 0 | #ifdef ENOTCONN |
270 | 0 | case ENOTCONN: |
271 | 0 | return G_IO_ERROR_NOT_CONNECTED; |
272 | 0 | break; |
273 | 0 | #endif |
274 | | |
275 | 0 | #ifdef EMSGSIZE |
276 | 0 | case EMSGSIZE: |
277 | 0 | return G_IO_ERROR_MESSAGE_TOO_LARGE; |
278 | 0 | break; |
279 | 0 | #endif |
280 | | |
281 | 0 | #ifdef ENOTSOCK |
282 | 0 | case ENOTSOCK: |
283 | 0 | return G_IO_ERROR_INVALID_ARGUMENT; |
284 | 0 | break; |
285 | 0 | #endif |
286 | | |
287 | 0 | default: |
288 | 0 | return G_IO_ERROR_FAILED; |
289 | 0 | break; |
290 | 0 | } |
291 | 0 | } |
292 | | |
293 | | #ifdef G_OS_WIN32 |
294 | | |
295 | | /** |
296 | | * g_io_error_from_win32_error: |
297 | | * @error_code: Windows error number. |
298 | | * |
299 | | * Converts some common error codes (as returned from GetLastError() |
300 | | * or WSAGetLastError()) into GIO error codes. The fallback value |
301 | | * %G_IO_ERROR_FAILED is returned for error codes not currently |
302 | | * handled (but note that future GLib releases may return a more |
303 | | * specific value instead). |
304 | | * |
305 | | * You can use g_win32_error_message() to get a localized string |
306 | | * corresponding to @error_code. (But note that unlike g_strerror(), |
307 | | * g_win32_error_message() returns a string that must be freed.) |
308 | | * |
309 | | * Returns: #GIOErrorEnum value for the given error number. |
310 | | * |
311 | | * Since: 2.26 |
312 | | **/ |
313 | | GIOErrorEnum |
314 | | g_io_error_from_win32_error (gint error_code) |
315 | | { |
316 | | /* Note: Winsock errors are a subset of Win32 error codes as a |
317 | | * whole. (The fact that the Winsock API makes them look like they |
318 | | * aren't is just because the API predates Win32.) |
319 | | */ |
320 | | |
321 | | switch (error_code) |
322 | | { |
323 | | case WSAEADDRINUSE: |
324 | | return G_IO_ERROR_ADDRESS_IN_USE; |
325 | | |
326 | | case WSAEWOULDBLOCK: |
327 | | return G_IO_ERROR_WOULD_BLOCK; |
328 | | |
329 | | case WSAEACCES: |
330 | | return G_IO_ERROR_PERMISSION_DENIED; |
331 | | |
332 | | case WSA_INVALID_HANDLE: |
333 | | case WSA_INVALID_PARAMETER: |
334 | | case WSAEINVAL: |
335 | | case WSAEBADF: |
336 | | case WSAENOTSOCK: |
337 | | return G_IO_ERROR_INVALID_ARGUMENT; |
338 | | |
339 | | case WSAEPROTONOSUPPORT: |
340 | | return G_IO_ERROR_NOT_SUPPORTED; |
341 | | |
342 | | case WSAECANCELLED: |
343 | | return G_IO_ERROR_CANCELLED; |
344 | | |
345 | | case WSAESOCKTNOSUPPORT: |
346 | | case WSAEOPNOTSUPP: |
347 | | case WSAEPFNOSUPPORT: |
348 | | case WSAEAFNOSUPPORT: |
349 | | return G_IO_ERROR_NOT_SUPPORTED; |
350 | | |
351 | | case WSAECONNRESET: |
352 | | case WSAENETRESET: |
353 | | case WSAESHUTDOWN: |
354 | | return G_IO_ERROR_CONNECTION_CLOSED; |
355 | | |
356 | | case WSAEHOSTUNREACH: |
357 | | return G_IO_ERROR_HOST_UNREACHABLE; |
358 | | |
359 | | case WSAENETUNREACH: |
360 | | return G_IO_ERROR_NETWORK_UNREACHABLE; |
361 | | |
362 | | case WSAECONNREFUSED: |
363 | | return G_IO_ERROR_CONNECTION_REFUSED; |
364 | | |
365 | | case WSAETIMEDOUT: |
366 | | return G_IO_ERROR_TIMED_OUT; |
367 | | |
368 | | case WSAENOTCONN: |
369 | | case ERROR_PIPE_LISTENING: |
370 | | return G_IO_ERROR_NOT_CONNECTED; |
371 | | |
372 | | case WSAEMSGSIZE: |
373 | | return G_IO_ERROR_MESSAGE_TOO_LARGE; |
374 | | |
375 | | default: |
376 | | return G_IO_ERROR_FAILED; |
377 | | } |
378 | | } |
379 | | |
380 | | #endif |