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, write to the
17 * Free Software Foundation, Inc., 59 Temple Place, Suite 330,
18 * Boston, MA 02111-1307, USA.
19 *
20 * Author: Alexander Larsson <alexl@redhat.com>
21 */
22
23 #include "config.h"
24 #include <errno.h>
25 #include "gioerror.h"
26
27 #include "gioalias.h"
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 * Return value: a #GQuark.
43 **/
44 GQuark
g_io_error_quark(void)45 g_io_error_quark (void)
46 {
47 return g_quark_from_static_string ("g-io-error-quark");
48 }
49
50 /**
51 * g_io_error_from_errno:
52 * @err_no: Error number as defined in errno.h.
53 *
54 * Converts errno.h error codes into GIO error codes.
55 *
56 * Returns: #GIOErrorEnum value for the given errno.h error number.
57 **/
58 GIOErrorEnum
g_io_error_from_errno(gint err_no)59 g_io_error_from_errno (gint err_no)
60 {
61 switch (err_no)
62 {
63 #ifdef EEXIST
64 case EEXIST:
65 return G_IO_ERROR_EXISTS;
66 break;
67 #endif
68
69 #ifdef EISDIR
70 case EISDIR:
71 return G_IO_ERROR_IS_DIRECTORY;
72 break;
73 #endif
74
75 #ifdef EACCES
76 case EACCES:
77 return G_IO_ERROR_PERMISSION_DENIED;
78 break;
79 #endif
80
81 #ifdef ENAMETOOLONG
82 case ENAMETOOLONG:
83 return G_IO_ERROR_FILENAME_TOO_LONG;
84 break;
85 #endif
86
87 #ifdef ENOENT
88 case ENOENT:
89 return G_IO_ERROR_NOT_FOUND;
90 break;
91 #endif
92
93 #ifdef ENOTDIR
94 case ENOTDIR:
95 return G_IO_ERROR_NOT_DIRECTORY;
96 break;
97 #endif
98
99 #ifdef EROFS
100 case EROFS:
101 return G_IO_ERROR_READ_ONLY;
102 break;
103 #endif
104
105 #ifdef ELOOP
106 case ELOOP:
107 return G_IO_ERROR_TOO_MANY_LINKS;
108 break;
109 #endif
110
111 #ifdef ENOSPC
112 case ENOSPC:
113 return G_IO_ERROR_NO_SPACE;
114 break;
115 #endif
116
117 #ifdef ENOMEM
118 case ENOMEM:
119 return G_IO_ERROR_NO_SPACE;
120 break;
121 #endif
122
123 #ifdef EINVAL
124 case EINVAL:
125 return G_IO_ERROR_INVALID_ARGUMENT;
126 break;
127 #endif
128
129 #ifdef EPERM
130 case EPERM:
131 return G_IO_ERROR_PERMISSION_DENIED;
132 break;
133 #endif
134
135 #ifdef ECANCELED
136 case ECANCELED:
137 return G_IO_ERROR_CANCELLED;
138 break;
139 #endif
140
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 #ifdef ETIMEDOUT
154 case ETIMEDOUT:
155 return G_IO_ERROR_TIMED_OUT;
156 break;
157 #endif
158
159 #ifdef EBUSY
160 case EBUSY:
161 return G_IO_ERROR_BUSY;
162 break;
163 #endif
164
165 /* some magic to deal with EWOULDBLOCK and EAGAIN.
166 * apparently on HP-UX these are actually defined to different values,
167 * but on Linux, for example, they are the same.
168 */
169 #if defined(EWOULDBLOCK) && defined(EAGAIN) && EWOULDBLOCK == EAGAIN
170 /* we have both and they are the same: only emit one case. */
171 case EAGAIN:
172 return G_IO_ERROR_WOULD_BLOCK;
173 break;
174 #else
175 /* else: consider each of them separately. this handles both the
176 * case of having only one and the case where they are different values.
177 */
178 # ifdef EAGAIN
179 case EAGAIN:
180 return G_IO_ERROR_WOULD_BLOCK;
181 break;
182 # endif
183
184 # ifdef EWOULDBLOCK
185 case EWOULDBLOCK:
186 return G_IO_ERROR_WOULD_BLOCK;
187 break;
188 # endif
189 #endif
190
191 #ifdef EMFILE
192 case EMFILE:
193 return G_IO_ERROR_TOO_MANY_OPEN_FILES;
194 break;
195 #endif
196
197 default:
198 return G_IO_ERROR_FAILED;
199 break;
200 }
201 }
202
203 #define __G_IO_ERROR_C__
204 #include "gioaliasdef.c"
205