Clarify expectations with error codes like G_IO_ERROR_FAILED
[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
26 /**
27  * SECTION:gioerror
28  * @short_description: Error helper functions
29  * @include: gio/gio.h
30  *
31  * Contains helper functions for reporting errors to the user.
32  **/
33
34 /**
35  * g_io_error_quark:
36  *
37  * Gets the GIO Error Quark.
38  *
39  * Returns: a #GQuark.
40  **/
41 G_DEFINE_QUARK (g-io-error-quark, g_io_error)
42
43 /**
44  * g_io_error_from_errno:
45  * @err_no: Error number as defined in errno.h.
46  *
47  * Converts errno.h error codes into GIO error codes. The fallback
48  * value %G_IO_ERROR_FAILED is returned for error codes not currently
49  * handled (but note that future GLib releases may return a more
50  * specific value instead).
51  *
52  * Returns: #GIOErrorEnum value for the given errno.h error number.
53  **/
54 GIOErrorEnum
55 g_io_error_from_errno (gint err_no)
56 {
57   switch (err_no)
58     {
59 #ifdef EEXIST
60     case EEXIST:
61       return G_IO_ERROR_EXISTS;
62       break;
63 #endif
64
65 #ifdef EISDIR
66     case EISDIR:
67       return G_IO_ERROR_IS_DIRECTORY;
68       break;
69 #endif
70
71 #ifdef EACCES
72     case EACCES:
73       return G_IO_ERROR_PERMISSION_DENIED;
74       break;
75 #endif
76
77 #ifdef ENAMETOOLONG
78     case ENAMETOOLONG:
79       return G_IO_ERROR_FILENAME_TOO_LONG;
80       break;
81 #endif
82
83 #ifdef ENOENT
84     case ENOENT:
85       return G_IO_ERROR_NOT_FOUND;
86       break;
87 #endif
88
89 #ifdef ENOTDIR
90     case ENOTDIR:
91       return G_IO_ERROR_NOT_DIRECTORY;
92       break;
93 #endif
94
95 #ifdef EROFS
96     case EROFS:
97       return G_IO_ERROR_READ_ONLY;
98       break;
99 #endif
100
101 #ifdef ELOOP
102     case ELOOP:
103       return G_IO_ERROR_TOO_MANY_LINKS;
104       break;
105 #endif
106
107 #ifdef ENOSPC
108     case ENOSPC:
109       return G_IO_ERROR_NO_SPACE;
110       break;
111 #endif
112
113 #ifdef ENOMEM
114     case ENOMEM:
115       return G_IO_ERROR_NO_SPACE;
116       break;
117 #endif
118       
119 #ifdef EINVAL
120     case EINVAL:
121       return G_IO_ERROR_INVALID_ARGUMENT;
122       break;
123 #endif
124
125 #ifdef EPERM
126     case EPERM:
127       return G_IO_ERROR_PERMISSION_DENIED;
128       break;
129 #endif
130
131 #ifdef ECANCELED
132     case ECANCELED:
133       return G_IO_ERROR_CANCELLED;
134       break;
135 #endif
136
137 #if defined(ENOTEMPTY) && (!defined (EEXIST) || (ENOTEMPTY != EEXIST))
138     case ENOTEMPTY:
139       return G_IO_ERROR_NOT_EMPTY;
140       break;
141 #endif
142
143 #ifdef ENOTSUP
144     case ENOTSUP:
145       return G_IO_ERROR_NOT_SUPPORTED;
146       break;
147 #endif
148
149 #ifdef ETIMEDOUT
150     case ETIMEDOUT:
151       return G_IO_ERROR_TIMED_OUT;
152       break;
153 #endif
154
155 #ifdef EBUSY
156     case EBUSY:
157       return G_IO_ERROR_BUSY;
158       break;
159 #endif
160
161 /* some magic to deal with EWOULDBLOCK and EAGAIN.
162  * apparently on HP-UX these are actually defined to different values,
163  * but on Linux, for example, they are the same.
164  */
165 #if defined(EWOULDBLOCK) && defined(EAGAIN) && EWOULDBLOCK == EAGAIN
166     /* we have both and they are the same: only emit one case. */
167     case EAGAIN:
168       return G_IO_ERROR_WOULD_BLOCK;
169       break;
170 #else
171     /* else: consider each of them separately.  this handles both the
172      * case of having only one and the case where they are different values.
173      */
174 # ifdef EAGAIN
175     case EAGAIN:
176       return G_IO_ERROR_WOULD_BLOCK;
177       break;
178 # endif
179
180 # ifdef EWOULDBLOCK
181     case EWOULDBLOCK:
182       return G_IO_ERROR_WOULD_BLOCK;
183       break;
184 # endif
185 #endif
186
187 #ifdef EMFILE
188     case EMFILE:
189       return G_IO_ERROR_TOO_MANY_OPEN_FILES;
190       break;
191 #endif
192
193 #ifdef EADDRINUSE
194     case EADDRINUSE:
195       return G_IO_ERROR_ADDRESS_IN_USE;
196       break;
197 #endif
198
199 #ifdef EHOSTUNREACH
200     case EHOSTUNREACH:
201       return G_IO_ERROR_HOST_UNREACHABLE;
202       break;
203 #endif
204
205 #ifdef ENETUNREACH
206     case ENETUNREACH:
207       return G_IO_ERROR_NETWORK_UNREACHABLE;
208       break;
209 #endif
210
211 #ifdef ECONNREFUSED
212     case ECONNREFUSED:
213       return G_IO_ERROR_CONNECTION_REFUSED;
214       break;
215 #endif
216
217 #ifdef EPIPE
218     case EPIPE:
219       return G_IO_ERROR_BROKEN_PIPE;
220       break;
221 #endif
222
223     default:
224       return G_IO_ERROR_FAILED;
225       break;
226     }
227 }
228
229 #ifdef G_OS_WIN32
230
231 /**
232  * g_io_error_from_win32_error:
233  * @error_code: Windows error number.
234  *
235  * Converts some common error codes into GIO error codes. The fallback
236  * value %G_IO_ERROR_FAILED is returned for error codes not currently
237  * handled (but note that future GLib releases may return a more
238  * specific value instead).
239  *
240  * Returns: #GIOErrorEnum value for the given error number.
241  *
242  * Since: 2.26
243  **/
244 GIOErrorEnum
245 g_io_error_from_win32_error (gint error_code)
246 {
247   switch (error_code)
248     {
249     default:
250       return G_IO_ERROR_FAILED;
251       break;
252     }
253 }
254
255 #endif