gspawn: Make error codes on Windows more specific
[glib.git] / gio / gioerror.c
blob900553e98f4ab2bc80d01ea0ecd484568a40a7d9
1 /* GIO - GLib Input, Output and Streaming Library
2 *
3 * Copyright (C) 2006-2007 Red Hat, Inc.
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.
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.
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/>.
18 * Author: Alexander Larsson <alexl@redhat.com>
21 #include "config.h"
22 #include <errno.h>
23 #include "gioerror.h"
25 #ifdef G_OS_WIN32
26 #include <winsock2.h>
27 #endif
29 /**
30 * SECTION:gioerror
31 * @short_description: Error helper functions
32 * @include: gio/gio.h
34 * Contains helper functions for reporting errors to the user.
35 **/
37 /**
38 * g_io_error_quark:
40 * Gets the GIO Error Quark.
42 * Returns: a #GQuark.
43 **/
44 G_DEFINE_QUARK (g-io-error-quark, g_io_error)
46 /**
47 * g_io_error_from_errno:
48 * @err_no: Error number as defined in errno.h.
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).
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;
61 * ret = read (blah);
62 * saved_errno = errno;
64 * g_io_error_from_errno (saved_errno);
65 * ]|
67 * Returns: #GIOErrorEnum value for the given errno.h error number.
68 **/
69 GIOErrorEnum
70 g_io_error_from_errno (gint err_no)
72 switch (err_no)
74 #ifdef EEXIST
75 case EEXIST:
76 return G_IO_ERROR_EXISTS;
77 break;
78 #endif
80 #ifdef EISDIR
81 case EISDIR:
82 return G_IO_ERROR_IS_DIRECTORY;
83 break;
84 #endif
86 #ifdef EACCES
87 case EACCES:
88 return G_IO_ERROR_PERMISSION_DENIED;
89 break;
90 #endif
92 #ifdef ENAMETOOLONG
93 case ENAMETOOLONG:
94 return G_IO_ERROR_FILENAME_TOO_LONG;
95 break;
96 #endif
98 #ifdef ENOENT
99 case ENOENT:
100 return G_IO_ERROR_NOT_FOUND;
101 break;
102 #endif
104 #ifdef ENOTDIR
105 case ENOTDIR:
106 return G_IO_ERROR_NOT_DIRECTORY;
107 break;
108 #endif
110 #ifdef EROFS
111 case EROFS:
112 return G_IO_ERROR_READ_ONLY;
113 break;
114 #endif
116 #ifdef ELOOP
117 case ELOOP:
118 return G_IO_ERROR_TOO_MANY_LINKS;
119 break;
120 #endif
122 #ifdef ENOSPC
123 case ENOSPC:
124 return G_IO_ERROR_NO_SPACE;
125 break;
126 #endif
128 #ifdef ENOMEM
129 case ENOMEM:
130 return G_IO_ERROR_NO_SPACE;
131 break;
132 #endif
134 #ifdef EINVAL
135 case EINVAL:
136 return G_IO_ERROR_INVALID_ARGUMENT;
137 break;
138 #endif
140 #ifdef EPERM
141 case EPERM:
142 return G_IO_ERROR_PERMISSION_DENIED;
143 break;
144 #endif
146 #ifdef ECANCELED
147 case ECANCELED:
148 return G_IO_ERROR_CANCELLED;
149 break;
150 #endif
152 /* ENOTEMPTY == EEXIST on AIX for backward compatibility reasons */
153 #if defined (ENOTEMPTY) && (!defined (EEXIST) || (ENOTEMPTY != EEXIST))
154 case ENOTEMPTY:
155 return G_IO_ERROR_NOT_EMPTY;
156 break;
157 #endif
159 #ifdef ENOTSUP
160 case ENOTSUP:
161 return G_IO_ERROR_NOT_SUPPORTED;
162 break;
163 #endif
165 /* EOPNOTSUPP == ENOTSUP on Linux, but POSIX considers them distinct */
166 #if defined (EOPNOTSUPP) && (!defined (ENOTSUP) || (EOPNOTSUPP != ENOTSUP))
167 case EOPNOTSUPP:
168 return G_IO_ERROR_NOT_SUPPORTED;
169 break;
170 #endif
172 #ifdef EPROTONOSUPPORT
173 case EPROTONOSUPPORT:
174 return G_IO_ERROR_NOT_SUPPORTED;
175 break;
176 #endif
178 #ifdef ESOCKTNOSUPPORT
179 case ESOCKTNOSUPPORT:
180 return G_IO_ERROR_NOT_SUPPORTED;
181 break;
182 #endif
184 #ifdef EPFNOSUPPORT
185 case EPFNOSUPPORT:
186 return G_IO_ERROR_NOT_SUPPORTED;
187 break;
188 #endif
190 #ifdef EAFNOSUPPORT
191 case EAFNOSUPPORT:
192 return G_IO_ERROR_NOT_SUPPORTED;
193 break;
194 #endif
196 #ifdef ETIMEDOUT
197 case ETIMEDOUT:
198 return G_IO_ERROR_TIMED_OUT;
199 break;
200 #endif
202 #ifdef EBUSY
203 case EBUSY:
204 return G_IO_ERROR_BUSY;
205 break;
206 #endif
208 #ifdef EWOULDBLOCK
209 case EWOULDBLOCK:
210 return G_IO_ERROR_WOULD_BLOCK;
211 break;
212 #endif
214 /* EWOULDBLOCK == EAGAIN on most systems, but POSIX considers them distinct */
215 #if defined (EAGAIN) && (!defined (EWOULDBLOCK) || (EWOULDBLOCK != EAGAIN))
216 case EAGAIN:
217 return G_IO_ERROR_WOULD_BLOCK;
218 break;
219 #endif
221 #ifdef EMFILE
222 case EMFILE:
223 return G_IO_ERROR_TOO_MANY_OPEN_FILES;
224 break;
225 #endif
227 #ifdef EADDRINUSE
228 case EADDRINUSE:
229 return G_IO_ERROR_ADDRESS_IN_USE;
230 break;
231 #endif
233 #ifdef EHOSTUNREACH
234 case EHOSTUNREACH:
235 return G_IO_ERROR_HOST_UNREACHABLE;
236 break;
237 #endif
239 #ifdef ENETUNREACH
240 case ENETUNREACH:
241 return G_IO_ERROR_NETWORK_UNREACHABLE;
242 break;
243 #endif
245 #ifdef ECONNREFUSED
246 case ECONNREFUSED:
247 return G_IO_ERROR_CONNECTION_REFUSED;
248 break;
249 #endif
251 #ifdef EPIPE
252 case EPIPE:
253 return G_IO_ERROR_BROKEN_PIPE;
254 break;
255 #endif
257 #ifdef ECONNRESET
258 case ECONNRESET:
259 return G_IO_ERROR_CONNECTION_CLOSED;
260 break;
261 #endif
263 #ifdef ENOTCONN
264 case ENOTCONN:
265 return G_IO_ERROR_NOT_CONNECTED;
266 break;
267 #endif
269 #ifdef EMSGSIZE
270 case EMSGSIZE:
271 return G_IO_ERROR_MESSAGE_TOO_LARGE;
272 break;
273 #endif
275 default:
276 return G_IO_ERROR_FAILED;
277 break;
281 #ifdef G_OS_WIN32
284 * g_io_error_from_win32_error:
285 * @error_code: Windows error number.
287 * Converts some common error codes (as returned from GetLastError()
288 * or WSAGetLastError()) into GIO error codes. The fallback value
289 * %G_IO_ERROR_FAILED is returned for error codes not currently
290 * handled (but note that future GLib releases may return a more
291 * specific value instead).
293 * You can use g_win32_error_message() to get a localized string
294 * corresponding to @error_code. (But note that unlike g_strerror(),
295 * g_win32_error_message() returns a string that must be freed.)
297 * Returns: #GIOErrorEnum value for the given error number.
299 * Since: 2.26
301 GIOErrorEnum
302 g_io_error_from_win32_error (gint error_code)
304 /* Note: Winsock errors are a subset of Win32 error codes as a
305 * whole. (The fact that the Winsock API makes them look like they
306 * aren't is just because the API predates Win32.)
309 switch (error_code)
311 case WSAEADDRINUSE:
312 return G_IO_ERROR_ADDRESS_IN_USE;
314 case WSAEWOULDBLOCK:
315 return G_IO_ERROR_WOULD_BLOCK;
317 case WSAEACCES:
318 return G_IO_ERROR_PERMISSION_DENIED;
320 case WSA_INVALID_HANDLE:
321 case WSA_INVALID_PARAMETER:
322 case WSAEINVAL:
323 case WSAEBADF:
324 case WSAENOTSOCK:
325 return G_IO_ERROR_INVALID_ARGUMENT;
327 case WSAEPROTONOSUPPORT:
328 return G_IO_ERROR_NOT_SUPPORTED;
330 case WSAECANCELLED:
331 return G_IO_ERROR_CANCELLED;
333 case WSAESOCKTNOSUPPORT:
334 case WSAEOPNOTSUPP:
335 case WSAEPFNOSUPPORT:
336 case WSAEAFNOSUPPORT:
337 return G_IO_ERROR_NOT_SUPPORTED;
339 case WSAECONNRESET:
340 case WSAESHUTDOWN:
341 return G_IO_ERROR_CONNECTION_CLOSED;
343 case WSAEHOSTUNREACH:
344 return G_IO_ERROR_HOST_UNREACHABLE;
346 case WSAENETUNREACH:
347 return G_IO_ERROR_NETWORK_UNREACHABLE;
349 case WSAECONNREFUSED:
350 return G_IO_ERROR_CONNECTION_REFUSED;
352 case WSAETIMEDOUT:
353 return G_IO_ERROR_TIMED_OUT;
355 case WSAENOTCONN:
356 case ERROR_PIPE_LISTENING:
357 return G_IO_ERROR_NOT_CONNECTED;
359 case WSAEMSGSIZE:
360 return G_IO_ERROR_MESSAGE_TOO_LARGE;
362 default:
363 return G_IO_ERROR_FAILED;
367 #endif