Blame gio/gioerror.c

Packit ae235b
/* GIO - GLib Input, Output and Streaming Library
Packit ae235b
 * 
Packit ae235b
 * Copyright (C) 2006-2007 Red Hat, Inc.
Packit ae235b
 *
Packit ae235b
 * This library is free software; you can redistribute it and/or
Packit ae235b
 * modify it under the terms of the GNU Lesser General Public
Packit ae235b
 * License as published by the Free Software Foundation; either
Packit ae235b
 * version 2.1 of the License, or (at your option) any later version.
Packit ae235b
 *
Packit ae235b
 * This library is distributed in the hope that it will be useful,
Packit ae235b
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit ae235b
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Packit ae235b
 * Lesser General Public License for more details.
Packit ae235b
 *
Packit ae235b
 * You should have received a copy of the GNU Lesser General
Packit ae235b
 * Public License along with this library; if not, see <http://www.gnu.org/licenses/>.
Packit ae235b
 *
Packit ae235b
 * Author: Alexander Larsson <alexl@redhat.com>
Packit ae235b
 */
Packit ae235b
Packit ae235b
#include "config.h"
Packit ae235b
#include <errno.h>
Packit ae235b
#include "gioerror.h"
Packit ae235b
Packit ae235b
#ifdef G_OS_WIN32
Packit ae235b
#include <winsock2.h>
Packit ae235b
#endif
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * SECTION:gioerror
Packit ae235b
 * @short_description: Error helper functions
Packit ae235b
 * @include: gio/gio.h
Packit ae235b
 *
Packit ae235b
 * Contains helper functions for reporting errors to the user.
Packit ae235b
 **/
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_io_error_quark:
Packit ae235b
 *
Packit ae235b
 * Gets the GIO Error Quark.
Packit ae235b
 *
Packit ae235b
 * Returns: a #GQuark.
Packit ae235b
 **/
Packit ae235b
G_DEFINE_QUARK (g-io-error-quark, g_io_error)
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_io_error_from_errno:
Packit ae235b
 * @err_no: Error number as defined in errno.h.
Packit ae235b
 *
Packit ae235b
 * Converts errno.h error codes into GIO error codes. The fallback
Packit ae235b
 * value %G_IO_ERROR_FAILED is returned for error codes not currently
Packit ae235b
 * handled (but note that future GLib releases may return a more
Packit ae235b
 * specific value instead).
Packit ae235b
 *
Packit ae235b
 * As %errno is global and may be modified by intermediate function
Packit ae235b
 * calls, you should save its value as soon as the call which sets it
Packit ae235b
 * returns:
Packit ae235b
 * |[
Packit ae235b
 *   int saved_errno;
Packit ae235b
 *
Packit ae235b
 *   ret = read (blah);
Packit ae235b
 *   saved_errno = errno;
Packit ae235b
 *
Packit ae235b
 *   g_io_error_from_errno (saved_errno);
Packit ae235b
 * ]|
Packit ae235b
 *
Packit ae235b
 * Returns: #GIOErrorEnum value for the given errno.h error number.
Packit ae235b
 **/
Packit ae235b
GIOErrorEnum
Packit ae235b
g_io_error_from_errno (gint err_no)
Packit ae235b
{
Packit ae235b
  switch (err_no)
Packit ae235b
    {
Packit ae235b
#ifdef EEXIST
Packit ae235b
    case EEXIST:
Packit ae235b
      return G_IO_ERROR_EXISTS;
Packit ae235b
      break;
Packit ae235b
#endif
Packit ae235b
Packit ae235b
#ifdef EISDIR
Packit ae235b
    case EISDIR:
Packit ae235b
      return G_IO_ERROR_IS_DIRECTORY;
Packit ae235b
      break;
Packit ae235b
#endif
Packit ae235b
Packit ae235b
#ifdef EACCES
Packit ae235b
    case EACCES:
Packit ae235b
      return G_IO_ERROR_PERMISSION_DENIED;
Packit ae235b
      break;
Packit ae235b
#endif
Packit ae235b
Packit ae235b
#ifdef ENAMETOOLONG
Packit ae235b
    case ENAMETOOLONG:
Packit ae235b
      return G_IO_ERROR_FILENAME_TOO_LONG;
Packit ae235b
      break;
Packit ae235b
#endif
Packit ae235b
Packit ae235b
#ifdef ENOENT
Packit ae235b
    case ENOENT:
Packit ae235b
      return G_IO_ERROR_NOT_FOUND;
Packit ae235b
      break;
Packit ae235b
#endif
Packit ae235b
Packit ae235b
#ifdef ENOTDIR
Packit ae235b
    case ENOTDIR:
Packit ae235b
      return G_IO_ERROR_NOT_DIRECTORY;
Packit ae235b
      break;
Packit ae235b
#endif
Packit ae235b
Packit ae235b
#ifdef EROFS
Packit ae235b
    case EROFS:
Packit ae235b
      return G_IO_ERROR_READ_ONLY;
Packit ae235b
      break;
Packit ae235b
#endif
Packit ae235b
Packit ae235b
#ifdef ELOOP
Packit ae235b
    case ELOOP:
Packit ae235b
      return G_IO_ERROR_TOO_MANY_LINKS;
Packit ae235b
      break;
Packit ae235b
#endif
Packit ae235b
Packit ae235b
#ifdef ENOSPC
Packit ae235b
    case ENOSPC:
Packit ae235b
      return G_IO_ERROR_NO_SPACE;
Packit ae235b
      break;
Packit ae235b
#endif
Packit ae235b
Packit ae235b
#ifdef ENOMEM
Packit ae235b
    case ENOMEM:
Packit ae235b
      return G_IO_ERROR_NO_SPACE;
Packit ae235b
      break;
Packit ae235b
#endif
Packit ae235b
      
Packit ae235b
#ifdef EINVAL
Packit ae235b
    case EINVAL:
Packit ae235b
      return G_IO_ERROR_INVALID_ARGUMENT;
Packit ae235b
      break;
Packit ae235b
#endif
Packit ae235b
Packit ae235b
#ifdef EPERM
Packit ae235b
    case EPERM:
Packit ae235b
      return G_IO_ERROR_PERMISSION_DENIED;
Packit ae235b
      break;
Packit ae235b
#endif
Packit ae235b
Packit ae235b
#ifdef ECANCELED
Packit ae235b
    case ECANCELED:
Packit ae235b
      return G_IO_ERROR_CANCELLED;
Packit ae235b
      break;
Packit ae235b
#endif
Packit ae235b
Packit ae235b
    /* ENOTEMPTY == EEXIST on AIX for backward compatibility reasons */
Packit ae235b
#if defined (ENOTEMPTY) && (!defined (EEXIST) || (ENOTEMPTY != EEXIST))
Packit ae235b
    case ENOTEMPTY:
Packit ae235b
      return G_IO_ERROR_NOT_EMPTY;
Packit ae235b
      break;
Packit ae235b
#endif
Packit ae235b
Packit ae235b
#ifdef ENOTSUP
Packit ae235b
    case ENOTSUP:
Packit ae235b
      return G_IO_ERROR_NOT_SUPPORTED;
Packit ae235b
      break;
Packit ae235b
#endif
Packit ae235b
Packit ae235b
    /* EOPNOTSUPP == ENOTSUP on Linux, but POSIX considers them distinct */
Packit ae235b
#if defined (EOPNOTSUPP) && (!defined (ENOTSUP) || (EOPNOTSUPP != ENOTSUP))
Packit ae235b
    case EOPNOTSUPP:
Packit ae235b
      return G_IO_ERROR_NOT_SUPPORTED;
Packit ae235b
      break;
Packit ae235b
#endif
Packit ae235b
Packit ae235b
#ifdef EPROTONOSUPPORT
Packit ae235b
    case EPROTONOSUPPORT:
Packit ae235b
      return G_IO_ERROR_NOT_SUPPORTED;
Packit ae235b
      break;
Packit ae235b
#endif
Packit ae235b
Packit ae235b
#ifdef ESOCKTNOSUPPORT
Packit ae235b
    case ESOCKTNOSUPPORT:
Packit ae235b
      return G_IO_ERROR_NOT_SUPPORTED;
Packit ae235b
      break;
Packit ae235b
#endif
Packit ae235b
Packit ae235b
#ifdef EPFNOSUPPORT
Packit ae235b
    case EPFNOSUPPORT:
Packit ae235b
      return G_IO_ERROR_NOT_SUPPORTED;
Packit ae235b
      break;
Packit ae235b
#endif
Packit ae235b
Packit ae235b
#ifdef EAFNOSUPPORT
Packit ae235b
    case EAFNOSUPPORT:
Packit ae235b
      return G_IO_ERROR_NOT_SUPPORTED;
Packit ae235b
      break;
Packit ae235b
#endif
Packit ae235b
Packit ae235b
#ifdef ETIMEDOUT
Packit ae235b
    case ETIMEDOUT:
Packit ae235b
      return G_IO_ERROR_TIMED_OUT;
Packit ae235b
      break;
Packit ae235b
#endif
Packit ae235b
Packit ae235b
#ifdef EBUSY
Packit ae235b
    case EBUSY:
Packit ae235b
      return G_IO_ERROR_BUSY;
Packit ae235b
      break;
Packit ae235b
#endif
Packit ae235b
Packit ae235b
#ifdef EWOULDBLOCK
Packit ae235b
    case EWOULDBLOCK:
Packit ae235b
      return G_IO_ERROR_WOULD_BLOCK;
Packit ae235b
      break;
Packit ae235b
#endif
Packit ae235b
Packit ae235b
    /* EWOULDBLOCK == EAGAIN on most systems, but POSIX considers them distinct */
Packit ae235b
#if defined (EAGAIN) && (!defined (EWOULDBLOCK) || (EWOULDBLOCK != EAGAIN))
Packit ae235b
    case EAGAIN:
Packit ae235b
      return G_IO_ERROR_WOULD_BLOCK;
Packit ae235b
      break;
Packit ae235b
#endif
Packit ae235b
Packit ae235b
#ifdef EMFILE
Packit ae235b
    case EMFILE:
Packit ae235b
      return G_IO_ERROR_TOO_MANY_OPEN_FILES;
Packit ae235b
      break;
Packit ae235b
#endif
Packit ae235b
Packit ae235b
#ifdef EADDRINUSE
Packit ae235b
    case EADDRINUSE:
Packit ae235b
      return G_IO_ERROR_ADDRESS_IN_USE;
Packit ae235b
      break;
Packit ae235b
#endif
Packit ae235b
Packit ae235b
#ifdef EHOSTUNREACH
Packit ae235b
    case EHOSTUNREACH:
Packit ae235b
      return G_IO_ERROR_HOST_UNREACHABLE;
Packit ae235b
      break;
Packit ae235b
#endif
Packit ae235b
Packit ae235b
#ifdef ENETUNREACH
Packit ae235b
    case ENETUNREACH:
Packit ae235b
      return G_IO_ERROR_NETWORK_UNREACHABLE;
Packit ae235b
      break;
Packit ae235b
#endif
Packit ae235b
Packit ae235b
#ifdef ECONNREFUSED
Packit ae235b
    case ECONNREFUSED:
Packit ae235b
      return G_IO_ERROR_CONNECTION_REFUSED;
Packit ae235b
      break;
Packit ae235b
#endif
Packit ae235b
Packit ae235b
#ifdef EPIPE
Packit ae235b
    case EPIPE:
Packit ae235b
      return G_IO_ERROR_BROKEN_PIPE;
Packit ae235b
      break;
Packit ae235b
#endif
Packit ae235b
Packit ae235b
#ifdef ECONNRESET
Packit ae235b
    case ECONNRESET:
Packit ae235b
      return G_IO_ERROR_CONNECTION_CLOSED;
Packit ae235b
      break;
Packit ae235b
#endif
Packit ae235b
Packit ae235b
#ifdef ENOTCONN
Packit ae235b
    case ENOTCONN:
Packit ae235b
      return G_IO_ERROR_NOT_CONNECTED;
Packit ae235b
      break;
Packit ae235b
#endif
Packit ae235b
Packit ae235b
#ifdef EMSGSIZE
Packit ae235b
    case EMSGSIZE:
Packit ae235b
      return G_IO_ERROR_MESSAGE_TOO_LARGE;
Packit ae235b
      break;
Packit ae235b
#endif
Packit ae235b
Packit ae235b
    default:
Packit ae235b
      return G_IO_ERROR_FAILED;
Packit ae235b
      break;
Packit ae235b
    }
Packit ae235b
}
Packit ae235b
Packit ae235b
#ifdef G_OS_WIN32
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_io_error_from_win32_error:
Packit ae235b
 * @error_code: Windows error number.
Packit ae235b
 *
Packit ae235b
 * Converts some common error codes (as returned from GetLastError()
Packit ae235b
 * or WSAGetLastError()) into GIO error codes. The fallback value
Packit ae235b
 * %G_IO_ERROR_FAILED is returned for error codes not currently
Packit ae235b
 * handled (but note that future GLib releases may return a more
Packit ae235b
 * specific value instead).
Packit ae235b
 *
Packit ae235b
 * You can use g_win32_error_message() to get a localized string
Packit ae235b
 * corresponding to @error_code. (But note that unlike g_strerror(),
Packit ae235b
 * g_win32_error_message() returns a string that must be freed.)
Packit ae235b
 *
Packit ae235b
 * Returns: #GIOErrorEnum value for the given error number.
Packit ae235b
 *
Packit ae235b
 * Since: 2.26
Packit ae235b
 **/
Packit ae235b
GIOErrorEnum
Packit ae235b
g_io_error_from_win32_error (gint error_code)
Packit ae235b
{
Packit ae235b
  /* Note: Winsock errors are a subset of Win32 error codes as a
Packit ae235b
   * whole. (The fact that the Winsock API makes them look like they
Packit ae235b
   * aren't is just because the API predates Win32.)
Packit ae235b
   */
Packit ae235b
Packit ae235b
  switch (error_code)
Packit ae235b
    {
Packit ae235b
    case WSAEADDRINUSE:
Packit ae235b
      return G_IO_ERROR_ADDRESS_IN_USE;
Packit ae235b
Packit ae235b
    case WSAEWOULDBLOCK:
Packit ae235b
      return G_IO_ERROR_WOULD_BLOCK;
Packit ae235b
Packit ae235b
    case WSAEACCES:
Packit ae235b
      return G_IO_ERROR_PERMISSION_DENIED;
Packit ae235b
Packit ae235b
    case WSA_INVALID_HANDLE:
Packit ae235b
    case WSA_INVALID_PARAMETER:
Packit ae235b
    case WSAEINVAL:
Packit ae235b
    case WSAEBADF:
Packit ae235b
    case WSAENOTSOCK:
Packit ae235b
      return G_IO_ERROR_INVALID_ARGUMENT;
Packit ae235b
Packit ae235b
    case WSAEPROTONOSUPPORT:
Packit ae235b
      return G_IO_ERROR_NOT_SUPPORTED;
Packit ae235b
Packit ae235b
    case WSAECANCELLED:
Packit ae235b
      return G_IO_ERROR_CANCELLED;
Packit ae235b
Packit ae235b
    case WSAESOCKTNOSUPPORT:
Packit ae235b
    case WSAEOPNOTSUPP:
Packit ae235b
    case WSAEPFNOSUPPORT:
Packit ae235b
    case WSAEAFNOSUPPORT:
Packit ae235b
      return G_IO_ERROR_NOT_SUPPORTED;
Packit ae235b
Packit ae235b
    case WSAECONNRESET:
Packit ae235b
    case WSAESHUTDOWN:
Packit ae235b
      return G_IO_ERROR_CONNECTION_CLOSED;
Packit ae235b
Packit ae235b
    case WSAEHOSTUNREACH:
Packit ae235b
      return G_IO_ERROR_HOST_UNREACHABLE;
Packit ae235b
Packit ae235b
    case WSAENETUNREACH:
Packit ae235b
      return G_IO_ERROR_NETWORK_UNREACHABLE;
Packit ae235b
Packit ae235b
    case WSAECONNREFUSED:
Packit ae235b
      return G_IO_ERROR_CONNECTION_REFUSED;
Packit ae235b
Packit ae235b
    case WSAETIMEDOUT:
Packit ae235b
      return G_IO_ERROR_TIMED_OUT;
Packit ae235b
Packit ae235b
    case WSAENOTCONN:
Packit ae235b
    case ERROR_PIPE_LISTENING:
Packit ae235b
      return G_IO_ERROR_NOT_CONNECTED;
Packit ae235b
Packit ae235b
    case WSAEMSGSIZE:
Packit ae235b
      return G_IO_ERROR_MESSAGE_TOO_LARGE;
Packit ae235b
Packit ae235b
    default:
Packit ae235b
      return G_IO_ERROR_FAILED;
Packit ae235b
    }
Packit ae235b
}
Packit ae235b
Packit ae235b
#endif