hook gvariant vectors up to kdbus
[platform/upstream/glib.git] / gio / gioerror.c
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 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  * Returns: #GIOErrorEnum value for the given errno.h error number.
56  **/
57 GIOErrorEnum
58 g_io_error_from_errno (gint err_no)
59 {
60   switch (err_no)
61     {
62 #ifdef EEXIST
63     case EEXIST:
64       return G_IO_ERROR_EXISTS;
65       break;
66 #endif
67
68 #ifdef EISDIR
69     case EISDIR:
70       return G_IO_ERROR_IS_DIRECTORY;
71       break;
72 #endif
73
74 #ifdef EACCES
75     case EACCES:
76       return G_IO_ERROR_PERMISSION_DENIED;
77       break;
78 #endif
79
80 #ifdef ENAMETOOLONG
81     case ENAMETOOLONG:
82       return G_IO_ERROR_FILENAME_TOO_LONG;
83       break;
84 #endif
85
86 #ifdef ENOENT
87     case ENOENT:
88       return G_IO_ERROR_NOT_FOUND;
89       break;
90 #endif
91
92 #ifdef ENOTDIR
93     case ENOTDIR:
94       return G_IO_ERROR_NOT_DIRECTORY;
95       break;
96 #endif
97
98 #ifdef EROFS
99     case EROFS:
100       return G_IO_ERROR_READ_ONLY;
101       break;
102 #endif
103
104 #ifdef ELOOP
105     case ELOOP:
106       return G_IO_ERROR_TOO_MANY_LINKS;
107       break;
108 #endif
109
110 #ifdef ENOSPC
111     case ENOSPC:
112       return G_IO_ERROR_NO_SPACE;
113       break;
114 #endif
115
116 #ifdef ENOMEM
117     case ENOMEM:
118       return G_IO_ERROR_NO_SPACE;
119       break;
120 #endif
121       
122 #ifdef EINVAL
123     case EINVAL:
124       return G_IO_ERROR_INVALID_ARGUMENT;
125       break;
126 #endif
127
128 #ifdef EPERM
129     case EPERM:
130       return G_IO_ERROR_PERMISSION_DENIED;
131       break;
132 #endif
133
134 #ifdef ECANCELED
135     case ECANCELED:
136       return G_IO_ERROR_CANCELLED;
137       break;
138 #endif
139
140     /* ENOTEMPTY == EEXIST on AIX for backward compatibility reasons */
141 #if defined (ENOTEMPTY) && (!defined (EEXIST) || (ENOTEMPTY != EEXIST))
142     case ENOTEMPTY:
143       return G_IO_ERROR_NOT_EMPTY;
144       break;
145 #endif
146
147 #ifdef ENOTSUP
148     case ENOTSUP:
149       return G_IO_ERROR_NOT_SUPPORTED;
150       break;
151 #endif
152
153     /* EOPNOTSUPP == ENOTSUP on Linux, but POSIX considers them distinct */
154 #if defined (EOPNOTSUPP) && (!defined (ENOTSUP) || (EOPNOTSUPP != ENOTSUP))
155     case EOPNOTSUPP:
156       return G_IO_ERROR_NOT_SUPPORTED;
157       break;
158 #endif
159
160 #ifdef EPROTONOSUPPORT
161     case EPROTONOSUPPORT:
162       return G_IO_ERROR_NOT_SUPPORTED;
163       break;
164 #endif
165
166 #ifdef ESOCKTNOSUPPORT
167     case ESOCKTNOSUPPORT:
168       return G_IO_ERROR_NOT_SUPPORTED;
169       break;
170 #endif
171
172 #ifdef EPFNOSUPPORT
173     case EPFNOSUPPORT:
174       return G_IO_ERROR_NOT_SUPPORTED;
175       break;
176 #endif
177
178 #ifdef EAFNOSUPPORT
179     case EAFNOSUPPORT:
180       return G_IO_ERROR_NOT_SUPPORTED;
181       break;
182 #endif
183
184 #ifdef ETIMEDOUT
185     case ETIMEDOUT:
186       return G_IO_ERROR_TIMED_OUT;
187       break;
188 #endif
189
190 #ifdef EBUSY
191     case EBUSY:
192       return G_IO_ERROR_BUSY;
193       break;
194 #endif
195
196 #ifdef EWOULDBLOCK
197     case EWOULDBLOCK:
198       return G_IO_ERROR_WOULD_BLOCK;
199       break;
200 #endif
201
202     /* EWOULDBLOCK == EAGAIN on most systems, but POSIX considers them distinct */
203 #if defined (EAGAIN) && (!defined (EWOULDBLOCK) || (EWOULDBLOCK != EAGAIN))
204     case EAGAIN:
205       return G_IO_ERROR_WOULD_BLOCK;
206       break;
207 #endif
208
209 #ifdef EMFILE
210     case EMFILE:
211       return G_IO_ERROR_TOO_MANY_OPEN_FILES;
212       break;
213 #endif
214
215 #ifdef EADDRINUSE
216     case EADDRINUSE:
217       return G_IO_ERROR_ADDRESS_IN_USE;
218       break;
219 #endif
220
221 #ifdef EHOSTUNREACH
222     case EHOSTUNREACH:
223       return G_IO_ERROR_HOST_UNREACHABLE;
224       break;
225 #endif
226
227 #ifdef ENETUNREACH
228     case ENETUNREACH:
229       return G_IO_ERROR_NETWORK_UNREACHABLE;
230       break;
231 #endif
232
233 #ifdef ECONNREFUSED
234     case ECONNREFUSED:
235       return G_IO_ERROR_CONNECTION_REFUSED;
236       break;
237 #endif
238
239 #ifdef EPIPE
240     case EPIPE:
241       return G_IO_ERROR_BROKEN_PIPE;
242       break;
243 #endif
244
245 #ifdef ECONNRESET
246     case ECONNRESET:
247       return G_IO_ERROR_CONNECTION_CLOSED;
248       break;
249 #endif
250
251 #ifdef ENOTCONN
252     case ENOTCONN:
253       return G_IO_ERROR_NOT_CONNECTED;
254       break;
255 #endif
256
257     default:
258       return G_IO_ERROR_FAILED;
259       break;
260     }
261 }
262
263 #ifdef G_OS_WIN32
264
265 /**
266  * g_io_error_from_win32_error:
267  * @error_code: Windows error number.
268  *
269  * Converts some common error codes (as returned from GetLastError()
270  * or WSAGetLastError()) into GIO error codes. The fallback value
271  * %G_IO_ERROR_FAILED is returned for error codes not currently
272  * handled (but note that future GLib releases may return a more
273  * specific value instead).
274  *
275  * You can use g_win32_error_message() to get a localized string
276  * corresponding to @error_code. (But note that unlike g_strerror(),
277  * g_win32_error_message() returns a string that must be freed.)
278  *
279  * Returns: #GIOErrorEnum value for the given error number.
280  *
281  * Since: 2.26
282  **/
283 GIOErrorEnum
284 g_io_error_from_win32_error (gint error_code)
285 {
286   /* Note: Winsock errors are a subset of Win32 error codes as a
287    * whole. (The fact that the Winsock API makes them look like they
288    * aren't is just because the API predates Win32.)
289    */
290
291   switch (error_code)
292     {
293     case WSAEADDRINUSE:
294       return G_IO_ERROR_ADDRESS_IN_USE;
295
296     case WSAEWOULDBLOCK:
297       return G_IO_ERROR_WOULD_BLOCK;
298
299     case WSAEACCES:
300       return G_IO_ERROR_PERMISSION_DENIED;
301
302     case WSA_INVALID_HANDLE:
303     case WSA_INVALID_PARAMETER:
304     case WSAEBADF:
305     case WSAENOTSOCK:
306       return G_IO_ERROR_INVALID_ARGUMENT;
307
308     case WSAEPROTONOSUPPORT:
309       return G_IO_ERROR_NOT_SUPPORTED;
310
311     case WSAECANCELLED:
312       return G_IO_ERROR_CANCELLED;
313
314     case WSAESOCKTNOSUPPORT:
315     case WSAEOPNOTSUPP:
316     case WSAEPFNOSUPPORT:
317     case WSAEAFNOSUPPORT:
318       return G_IO_ERROR_NOT_SUPPORTED;
319
320     case WSAECONNRESET:
321       return G_IO_ERROR_CONNECTION_CLOSED;
322
323     case ERROR_PIPE_LISTENING:
324       return G_IO_ERROR_NOT_CONNECTED;
325
326     default:
327       return G_IO_ERROR_FAILED;
328     }
329 }
330
331 #endif