Coverage Report

Created: 2025-07-23 08:13

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