2007-11-26 17:13:05 +01:00
|
|
|
/* GIO - GLib Input, Output and Streaming Library
|
|
|
|
*
|
|
|
|
* Copyright (C) 2006-2007 Red Hat, Inc.
|
2022-06-14 19:45:53 +02:00
|
|
|
* Copyright (C) 2022 Canonical Ltd.
|
2007-11-26 17:13:05 +01:00
|
|
|
*
|
2022-05-18 10:12:45 +02:00
|
|
|
* SPDX-License-Identifier: LGPL-2.1-or-later
|
|
|
|
*
|
2007-11-26 17:13:05 +01:00
|
|
|
* This library is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU Lesser General Public
|
|
|
|
* License as published by the Free Software Foundation; either
|
2017-05-27 18:21:30 +02:00
|
|
|
* version 2.1 of the License, or (at your option) any later version.
|
2007-11-26 17:13:05 +01:00
|
|
|
*
|
|
|
|
* This library is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
* Lesser General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Lesser General
|
2014-01-23 12:58:29 +01:00
|
|
|
* Public License along with this library; if not, see <http://www.gnu.org/licenses/>.
|
2007-11-26 17:13:05 +01:00
|
|
|
*
|
|
|
|
* Author: Alexander Larsson <alexl@redhat.com>
|
2022-06-14 19:45:53 +02:00
|
|
|
* Author: Marco Trevisan <marco.trevisan@canonical.com>
|
2007-11-26 17:13:05 +01:00
|
|
|
*/
|
|
|
|
|
2008-06-22 17:10:51 +02:00
|
|
|
#include "config.h"
|
2007-11-26 17:13:05 +01:00
|
|
|
#include <errno.h>
|
|
|
|
#include "gioerror.h"
|
|
|
|
|
2014-03-25 18:52:45 +01:00
|
|
|
#ifdef G_OS_WIN32
|
|
|
|
#include <winsock2.h>
|
|
|
|
#endif
|
2007-11-28 13:39:07 +01:00
|
|
|
|
2007-11-26 17:13:05 +01:00
|
|
|
/**
|
|
|
|
* g_io_error_quark:
|
2012-08-28 05:42:12 +02:00
|
|
|
*
|
2007-11-27 15:00:13 +01:00
|
|
|
* Gets the GIO Error Quark.
|
2007-11-26 17:13:05 +01:00
|
|
|
*
|
2014-02-20 01:35:23 +01:00
|
|
|
* Returns: a #GQuark.
|
2007-11-26 17:13:05 +01:00
|
|
|
**/
|
2012-08-28 19:15:29 +02:00
|
|
|
G_DEFINE_QUARK (g-io-error-quark, g_io_error)
|
2007-11-26 17:13:05 +01:00
|
|
|
|
2007-11-27 15:00:13 +01:00
|
|
|
/**
|
|
|
|
* g_io_error_from_errno:
|
|
|
|
* @err_no: Error number as defined in errno.h.
|
|
|
|
*
|
2023-10-14 19:09:43 +02:00
|
|
|
* Converts `errno.h` error codes into GIO error codes.
|
|
|
|
*
|
|
|
|
* The fallback value %G_IO_ERROR_FAILED is returned for error codes not
|
|
|
|
* currently handled (but note that future GLib releases may return a more
|
2014-03-20 14:25:19 +01:00
|
|
|
* specific value instead).
|
2007-11-27 15:00:13 +01:00
|
|
|
*
|
2023-10-14 19:09:43 +02:00
|
|
|
* As `errno` is global and may be modified by intermediate function
|
|
|
|
* calls, you should save its value immediately after the call returns,
|
|
|
|
* and use the saved value instead of `errno`:
|
|
|
|
*
|
|
|
|
*
|
|
|
|
* |[<!-- language="C" -->
|
2017-07-31 13:16:44 +02:00
|
|
|
* int saved_errno;
|
|
|
|
*
|
|
|
|
* ret = read (blah);
|
|
|
|
* saved_errno = errno;
|
|
|
|
*
|
|
|
|
* g_io_error_from_errno (saved_errno);
|
|
|
|
* ]|
|
|
|
|
*
|
2023-10-14 19:09:43 +02:00
|
|
|
* Returns: #GIOErrorEnum value for the given `errno.h` error number
|
|
|
|
*/
|
2007-11-26 17:13:05 +01:00
|
|
|
GIOErrorEnum
|
|
|
|
g_io_error_from_errno (gint err_no)
|
|
|
|
{
|
2022-06-22 19:51:41 +02:00
|
|
|
GFileError file_error;
|
|
|
|
GIOErrorEnum io_error;
|
2007-11-26 17:13:05 +01:00
|
|
|
|
2022-06-22 19:51:41 +02:00
|
|
|
file_error = g_file_error_from_errno (err_no);
|
|
|
|
io_error = g_io_error_from_file_error (file_error);
|
2021-02-26 14:25:50 +01:00
|
|
|
|
2022-06-22 19:51:41 +02:00
|
|
|
if (io_error != G_IO_ERROR_FAILED)
|
|
|
|
return io_error;
|
2007-11-26 17:13:05 +01:00
|
|
|
|
2022-06-22 19:51:41 +02:00
|
|
|
switch (err_no)
|
|
|
|
{
|
2022-06-14 18:26:05 +02:00
|
|
|
#ifdef EMLINK
|
|
|
|
case EMLINK:
|
|
|
|
return G_IO_ERROR_TOO_MANY_LINKS;
|
|
|
|
break;
|
|
|
|
#endif
|
|
|
|
|
2022-06-14 19:07:39 +02:00
|
|
|
#ifdef ENOMSG
|
|
|
|
case ENOMSG:
|
|
|
|
return G_IO_ERROR_INVALID_DATA;
|
|
|
|
break;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef ENODATA
|
|
|
|
case ENODATA:
|
|
|
|
return G_IO_ERROR_INVALID_DATA;
|
|
|
|
break;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef EBADMSG
|
|
|
|
case EBADMSG:
|
|
|
|
return G_IO_ERROR_INVALID_DATA;
|
|
|
|
break;
|
2007-11-26 17:13:05 +01:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef ECANCELED
|
|
|
|
case ECANCELED:
|
|
|
|
return G_IO_ERROR_CANCELLED;
|
|
|
|
break;
|
|
|
|
#endif
|
|
|
|
|
2014-03-21 21:54:04 +01:00
|
|
|
/* ENOTEMPTY == EEXIST on AIX for backward compatibility reasons */
|
|
|
|
#if defined (ENOTEMPTY) && (!defined (EEXIST) || (ENOTEMPTY != EEXIST))
|
2007-11-26 17:13:05 +01:00
|
|
|
case ENOTEMPTY:
|
|
|
|
return G_IO_ERROR_NOT_EMPTY;
|
|
|
|
break;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef ENOTSUP
|
|
|
|
case ENOTSUP:
|
|
|
|
return G_IO_ERROR_NOT_SUPPORTED;
|
|
|
|
break;
|
|
|
|
#endif
|
|
|
|
|
2014-03-21 21:54:04 +01:00
|
|
|
/* EOPNOTSUPP == ENOTSUP on Linux, but POSIX considers them distinct */
|
|
|
|
#if defined (EOPNOTSUPP) && (!defined (ENOTSUP) || (EOPNOTSUPP != ENOTSUP))
|
|
|
|
case EOPNOTSUPP:
|
|
|
|
return G_IO_ERROR_NOT_SUPPORTED;
|
|
|
|
break;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef EPROTONOSUPPORT
|
|
|
|
case EPROTONOSUPPORT:
|
|
|
|
return G_IO_ERROR_NOT_SUPPORTED;
|
|
|
|
break;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef ESOCKTNOSUPPORT
|
|
|
|
case ESOCKTNOSUPPORT:
|
|
|
|
return G_IO_ERROR_NOT_SUPPORTED;
|
|
|
|
break;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef EPFNOSUPPORT
|
|
|
|
case EPFNOSUPPORT:
|
|
|
|
return G_IO_ERROR_NOT_SUPPORTED;
|
|
|
|
break;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef EAFNOSUPPORT
|
|
|
|
case EAFNOSUPPORT:
|
|
|
|
return G_IO_ERROR_NOT_SUPPORTED;
|
2022-06-14 19:35:24 +02:00
|
|
|
break;
|
|
|
|
#endif
|
|
|
|
|
2007-11-26 17:13:05 +01:00
|
|
|
#ifdef ETIMEDOUT
|
|
|
|
case ETIMEDOUT:
|
|
|
|
return G_IO_ERROR_TIMED_OUT;
|
|
|
|
break;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef EBUSY
|
|
|
|
case EBUSY:
|
|
|
|
return G_IO_ERROR_BUSY;
|
|
|
|
break;
|
|
|
|
#endif
|
|
|
|
|
2014-03-21 21:54:04 +01:00
|
|
|
#ifdef EWOULDBLOCK
|
|
|
|
case EWOULDBLOCK:
|
2009-01-29 16:55:11 +01:00
|
|
|
return G_IO_ERROR_WOULD_BLOCK;
|
|
|
|
break;
|
2014-03-21 21:54:04 +01:00
|
|
|
#endif
|
2009-01-29 16:55:11 +01:00
|
|
|
|
2014-03-21 21:54:04 +01:00
|
|
|
/* EWOULDBLOCK == EAGAIN on most systems, but POSIX considers them distinct */
|
|
|
|
#if defined (EAGAIN) && (!defined (EWOULDBLOCK) || (EWOULDBLOCK != EAGAIN))
|
|
|
|
case EAGAIN:
|
2007-11-26 17:13:05 +01:00
|
|
|
return G_IO_ERROR_WOULD_BLOCK;
|
|
|
|
break;
|
|
|
|
#endif
|
2009-01-08 02:32:15 +01:00
|
|
|
|
2009-05-14 15:26:37 +02:00
|
|
|
#ifdef EADDRINUSE
|
|
|
|
case EADDRINUSE:
|
|
|
|
return G_IO_ERROR_ADDRESS_IN_USE;
|
|
|
|
break;
|
|
|
|
#endif
|
|
|
|
|
2010-04-28 21:39:56 +02:00
|
|
|
#ifdef EHOSTUNREACH
|
|
|
|
case EHOSTUNREACH:
|
|
|
|
return G_IO_ERROR_HOST_UNREACHABLE;
|
|
|
|
break;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef ENETUNREACH
|
|
|
|
case ENETUNREACH:
|
|
|
|
return G_IO_ERROR_NETWORK_UNREACHABLE;
|
|
|
|
break;
|
|
|
|
#endif
|
|
|
|
|
2022-06-14 19:14:35 +02:00
|
|
|
#ifdef ENETDOWN
|
|
|
|
case ENETDOWN:
|
|
|
|
return G_IO_ERROR_NETWORK_UNREACHABLE;
|
|
|
|
break;
|
|
|
|
#endif
|
|
|
|
|
2010-04-28 21:39:56 +02:00
|
|
|
#ifdef ECONNREFUSED
|
|
|
|
case ECONNREFUSED:
|
|
|
|
return G_IO_ERROR_CONNECTION_REFUSED;
|
|
|
|
break;
|
|
|
|
#endif
|
|
|
|
|
2011-08-19 16:23:12 +02:00
|
|
|
#ifdef ECONNRESET
|
|
|
|
case ECONNRESET:
|
|
|
|
return G_IO_ERROR_CONNECTION_CLOSED;
|
|
|
|
break;
|
|
|
|
#endif
|
|
|
|
|
2014-12-02 14:25:56 +01:00
|
|
|
#ifdef ENOTCONN
|
|
|
|
case ENOTCONN:
|
|
|
|
return G_IO_ERROR_NOT_CONNECTED;
|
|
|
|
break;
|
|
|
|
#endif
|
|
|
|
|
2023-11-06 13:01:13 +01:00
|
|
|
#ifdef EDESTADDRREQ
|
|
|
|
case EDESTADDRREQ:
|
|
|
|
return G_IO_ERROR_DESTINATION_UNSET;
|
|
|
|
break;
|
|
|
|
#endif
|
|
|
|
|
2015-07-02 12:32:34 +02:00
|
|
|
#ifdef EMSGSIZE
|
|
|
|
case EMSGSIZE:
|
|
|
|
return G_IO_ERROR_MESSAGE_TOO_LARGE;
|
|
|
|
break;
|
|
|
|
#endif
|
|
|
|
|
2019-03-16 16:58:16 +01:00
|
|
|
#ifdef ENOTSOCK
|
|
|
|
case ENOTSOCK:
|
|
|
|
return G_IO_ERROR_INVALID_ARGUMENT;
|
|
|
|
break;
|
|
|
|
#endif
|
|
|
|
|
2007-11-26 17:13:05 +01:00
|
|
|
default:
|
|
|
|
return G_IO_ERROR_FAILED;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2007-11-28 13:39:07 +01:00
|
|
|
|
2022-06-14 19:45:53 +02:00
|
|
|
/**
|
|
|
|
* g_io_error_from_file_error:
|
|
|
|
* @file_error: a #GFileError.
|
|
|
|
*
|
|
|
|
* Converts #GFileError error codes into GIO error codes.
|
|
|
|
*
|
|
|
|
* Returns: #GIOErrorEnum value for the given #GFileError error value.
|
|
|
|
*
|
|
|
|
* Since: 2.74
|
|
|
|
**/
|
|
|
|
GIOErrorEnum
|
|
|
|
g_io_error_from_file_error (GFileError file_error)
|
|
|
|
{
|
|
|
|
switch (file_error)
|
|
|
|
{
|
|
|
|
case G_FILE_ERROR_EXIST:
|
|
|
|
return G_IO_ERROR_EXISTS;
|
|
|
|
case G_FILE_ERROR_ISDIR:
|
|
|
|
return G_IO_ERROR_IS_DIRECTORY;
|
|
|
|
case G_FILE_ERROR_ACCES:
|
|
|
|
return G_IO_ERROR_PERMISSION_DENIED;
|
|
|
|
case G_FILE_ERROR_NAMETOOLONG:
|
|
|
|
return G_IO_ERROR_FILENAME_TOO_LONG;
|
|
|
|
case G_FILE_ERROR_NOENT:
|
|
|
|
return G_IO_ERROR_NOT_FOUND;
|
|
|
|
case G_FILE_ERROR_NOTDIR:
|
|
|
|
return G_IO_ERROR_NOT_DIRECTORY;
|
|
|
|
case G_FILE_ERROR_NXIO:
|
|
|
|
return G_IO_ERROR_NOT_REGULAR_FILE;
|
|
|
|
case G_FILE_ERROR_NODEV:
|
|
|
|
return G_IO_ERROR_NO_SUCH_DEVICE;
|
|
|
|
case G_FILE_ERROR_ROFS:
|
|
|
|
return G_IO_ERROR_READ_ONLY;
|
|
|
|
case G_FILE_ERROR_TXTBSY:
|
|
|
|
return G_IO_ERROR_BUSY;
|
|
|
|
case G_FILE_ERROR_LOOP:
|
|
|
|
return G_IO_ERROR_TOO_MANY_LINKS;
|
|
|
|
case G_FILE_ERROR_NOSPC:
|
|
|
|
case G_FILE_ERROR_NOMEM:
|
|
|
|
return G_IO_ERROR_NO_SPACE;
|
|
|
|
case G_FILE_ERROR_MFILE:
|
|
|
|
case G_FILE_ERROR_NFILE:
|
|
|
|
return G_IO_ERROR_TOO_MANY_OPEN_FILES;
|
|
|
|
case G_FILE_ERROR_INVAL:
|
|
|
|
return G_IO_ERROR_INVALID_ARGUMENT;
|
|
|
|
case G_FILE_ERROR_PIPE:
|
|
|
|
return G_IO_ERROR_BROKEN_PIPE;
|
|
|
|
case G_FILE_ERROR_AGAIN:
|
|
|
|
return G_IO_ERROR_WOULD_BLOCK;
|
|
|
|
case G_FILE_ERROR_PERM:
|
|
|
|
return G_IO_ERROR_PERMISSION_DENIED;
|
|
|
|
case G_FILE_ERROR_NOSYS:
|
|
|
|
return G_IO_ERROR_NOT_SUPPORTED;
|
|
|
|
case G_FILE_ERROR_BADF:
|
|
|
|
case G_FILE_ERROR_FAILED:
|
|
|
|
case G_FILE_ERROR_FAULT:
|
|
|
|
case G_FILE_ERROR_INTR:
|
|
|
|
case G_FILE_ERROR_IO:
|
|
|
|
return G_IO_ERROR_FAILED;
|
|
|
|
default:
|
|
|
|
g_return_val_if_reached (G_IO_ERROR_FAILED);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-04-19 10:32:05 +02:00
|
|
|
#ifdef G_OS_WIN32
|
|
|
|
|
|
|
|
/**
|
|
|
|
* g_io_error_from_win32_error:
|
|
|
|
* @error_code: Windows error number.
|
|
|
|
*
|
2014-03-25 18:52:45 +01:00
|
|
|
* Converts some common error codes (as returned from GetLastError()
|
|
|
|
* or WSAGetLastError()) into GIO error codes. The fallback value
|
|
|
|
* %G_IO_ERROR_FAILED is returned for error codes not currently
|
2014-03-20 14:25:19 +01:00
|
|
|
* handled (but note that future GLib releases may return a more
|
|
|
|
* specific value instead).
|
2010-04-19 10:32:05 +02:00
|
|
|
*
|
2014-03-25 18:52:45 +01:00
|
|
|
* You can use g_win32_error_message() to get a localized string
|
|
|
|
* corresponding to @error_code. (But note that unlike g_strerror(),
|
|
|
|
* g_win32_error_message() returns a string that must be freed.)
|
|
|
|
*
|
2010-04-19 10:32:05 +02:00
|
|
|
* Returns: #GIOErrorEnum value for the given error number.
|
|
|
|
*
|
|
|
|
* Since: 2.26
|
|
|
|
**/
|
|
|
|
GIOErrorEnum
|
|
|
|
g_io_error_from_win32_error (gint error_code)
|
|
|
|
{
|
2014-03-25 18:52:45 +01:00
|
|
|
/* Note: Winsock errors are a subset of Win32 error codes as a
|
|
|
|
* whole. (The fact that the Winsock API makes them look like they
|
|
|
|
* aren't is just because the API predates Win32.)
|
|
|
|
*/
|
|
|
|
|
2010-04-19 10:32:05 +02:00
|
|
|
switch (error_code)
|
|
|
|
{
|
2014-03-25 18:52:45 +01:00
|
|
|
case WSAEADDRINUSE:
|
|
|
|
return G_IO_ERROR_ADDRESS_IN_USE;
|
|
|
|
|
|
|
|
case WSAEWOULDBLOCK:
|
|
|
|
return G_IO_ERROR_WOULD_BLOCK;
|
|
|
|
|
|
|
|
case WSAEACCES:
|
|
|
|
return G_IO_ERROR_PERMISSION_DENIED;
|
|
|
|
|
|
|
|
case WSA_INVALID_HANDLE:
|
|
|
|
case WSA_INVALID_PARAMETER:
|
2015-09-04 10:58:57 +02:00
|
|
|
case WSAEINVAL:
|
2014-03-25 18:52:45 +01:00
|
|
|
case WSAEBADF:
|
|
|
|
case WSAENOTSOCK:
|
|
|
|
return G_IO_ERROR_INVALID_ARGUMENT;
|
|
|
|
|
|
|
|
case WSAEPROTONOSUPPORT:
|
|
|
|
return G_IO_ERROR_NOT_SUPPORTED;
|
|
|
|
|
|
|
|
case WSAECANCELLED:
|
|
|
|
return G_IO_ERROR_CANCELLED;
|
|
|
|
|
|
|
|
case WSAESOCKTNOSUPPORT:
|
|
|
|
case WSAEOPNOTSUPP:
|
|
|
|
case WSAEPFNOSUPPORT:
|
|
|
|
case WSAEAFNOSUPPORT:
|
|
|
|
return G_IO_ERROR_NOT_SUPPORTED;
|
|
|
|
|
2011-08-19 16:23:12 +02:00
|
|
|
case WSAECONNRESET:
|
2020-08-17 12:11:22 +02:00
|
|
|
case WSAENETRESET:
|
2016-12-05 18:25:52 +01:00
|
|
|
case WSAESHUTDOWN:
|
2011-08-19 16:23:12 +02:00
|
|
|
return G_IO_ERROR_CONNECTION_CLOSED;
|
|
|
|
|
2015-09-04 10:58:57 +02:00
|
|
|
case WSAEHOSTUNREACH:
|
|
|
|
return G_IO_ERROR_HOST_UNREACHABLE;
|
|
|
|
|
|
|
|
case WSAENETUNREACH:
|
|
|
|
return G_IO_ERROR_NETWORK_UNREACHABLE;
|
|
|
|
|
|
|
|
case WSAECONNREFUSED:
|
|
|
|
return G_IO_ERROR_CONNECTION_REFUSED;
|
|
|
|
|
|
|
|
case WSAETIMEDOUT:
|
|
|
|
return G_IO_ERROR_TIMED_OUT;
|
|
|
|
|
|
|
|
case WSAENOTCONN:
|
2014-12-02 13:50:51 +01:00
|
|
|
case ERROR_PIPE_LISTENING:
|
|
|
|
return G_IO_ERROR_NOT_CONNECTED;
|
|
|
|
|
2015-07-02 12:32:34 +02:00
|
|
|
case WSAEMSGSIZE:
|
|
|
|
return G_IO_ERROR_MESSAGE_TOO_LARGE;
|
|
|
|
|
2010-04-19 10:32:05 +02:00
|
|
|
default:
|
|
|
|
return G_IO_ERROR_FAILED;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|