Coverage Report

Created: 2025-06-13 06:55

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