• Main Page
  • Related Pages
  • Modules
  • Data Structures
  • Files
  • File List

dbus-errors.c

00001 /* -*- mode: C; c-file-style: "gnu"; indent-tabs-mode: nil; -*- */
00002 /* dbus-errors.c Error reporting
00003  *
00004  * Copyright (C) 2002, 2004  Red Hat Inc.
00005  * Copyright (C) 2003  CodeFactory AB
00006  *
00007  * Licensed under the Academic Free License version 2.1
00008  * 
00009  * This program is free software; you can redistribute it and/or modify
00010  * it under the terms of the GNU General Public License as published by
00011  * the Free Software Foundation; either version 2 of the License, or
00012  * (at your option) any later version.
00013  *
00014  * This program is distributed in the hope that it will be useful,
00015  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00016  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00017  * GNU General Public License for more details.
00018  * 
00019  * You should have received a copy of the GNU General Public License
00020  * along with this program; if not, write to the Free Software
00021  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
00022  *
00023  */
00024 
00025 #include <config.h>
00026 #include "dbus-errors.h"
00027 #include "dbus-internals.h"
00028 #include "dbus-string.h"
00029 #include "dbus-protocol.h"
00030 #include <stdarg.h>
00031 #include <string.h>
00032 
00065 typedef struct
00066 {
00067   char *name; 
00068   char *message; 
00070   unsigned int const_message : 1; 
00072   unsigned int dummy2 : 1; 
00073   unsigned int dummy3 : 1; 
00074   unsigned int dummy4 : 1; 
00075   unsigned int dummy5 : 1; 
00077   void *padding1; 
00079 } DBusRealError;
00080 
00089 static const char*
00090 message_from_error (const char *error)
00091 {
00092   if (strcmp (error, DBUS_ERROR_FAILED) == 0)
00093     return "Unknown error";
00094   else if (strcmp (error, DBUS_ERROR_NO_MEMORY) == 0)
00095     return "Not enough memory available";
00096   else if (strcmp (error, DBUS_ERROR_IO_ERROR) == 0)
00097     return "Error reading or writing data";
00098   else if (strcmp (error, DBUS_ERROR_BAD_ADDRESS) == 0)
00099     return "Could not parse address";
00100   else if (strcmp (error, DBUS_ERROR_NOT_SUPPORTED) == 0)
00101     return "Feature not supported";
00102   else if (strcmp (error, DBUS_ERROR_LIMITS_EXCEEDED) == 0)
00103     return "Resource limits exceeded";
00104   else if (strcmp (error, DBUS_ERROR_ACCESS_DENIED) == 0)
00105     return "Permission denied";
00106   else if (strcmp (error, DBUS_ERROR_AUTH_FAILED) == 0)
00107     return "Could not authenticate to server";
00108   else if (strcmp (error, DBUS_ERROR_NO_SERVER) == 0)
00109     return "No server available at address";
00110   else if (strcmp (error, DBUS_ERROR_TIMEOUT) == 0)
00111     return "Connection timed out";
00112   else if (strcmp (error, DBUS_ERROR_NO_NETWORK) == 0)
00113     return "Network unavailable";
00114   else if (strcmp (error, DBUS_ERROR_ADDRESS_IN_USE) == 0)
00115     return "Address already in use";
00116   else if (strcmp (error, DBUS_ERROR_DISCONNECTED) == 0)
00117     return "Disconnected.";
00118   else if (strcmp (error, DBUS_ERROR_INVALID_ARGS) == 0)
00119     return "Invalid arguments.";
00120   else if (strcmp (error, DBUS_ERROR_NO_REPLY) == 0)
00121     return "Did not get a reply message.";
00122   else if (strcmp (error, DBUS_ERROR_FILE_NOT_FOUND) == 0)
00123     return "File doesn't exist.";
00124   else if (strcmp (error, DBUS_ERROR_OBJECT_PATH_IN_USE) == 0)
00125     return "Object path already in use";
00126   else
00127     return error;
00128 }
00129  /* End of internals */
00131 
00185 void
00186 dbus_error_init (DBusError *error)
00187 {
00188   DBusRealError *real;
00189 
00190   _dbus_return_if_fail (error != NULL);
00191 
00192   _dbus_assert (sizeof (DBusError) == sizeof (DBusRealError));
00193 
00194   real = (DBusRealError *)error;
00195   
00196   real->name = NULL;  
00197   real->message = NULL;
00198 
00199   real->const_message = TRUE;
00200 }
00201 
00208 void
00209 dbus_error_free (DBusError *error)
00210 {
00211   DBusRealError *real;
00212 
00213   _dbus_return_if_fail (error != NULL);
00214   
00215   real = (DBusRealError *)error;
00216 
00217   if (!real->const_message)
00218     {
00219       dbus_free (real->name);
00220       dbus_free (real->message);
00221     }
00222 
00223   dbus_error_init (error);
00224 }
00225 
00240 void
00241 dbus_set_error_const (DBusError  *error,
00242                       const char *name,
00243                       const char *message)
00244 {
00245   DBusRealError *real;
00246 
00247   _dbus_return_if_error_is_set (error);
00248   _dbus_return_if_fail (name != NULL);
00249   
00250   if (error == NULL)
00251     return;
00252 
00253   _dbus_assert (error->name == NULL);
00254   _dbus_assert (error->message == NULL);
00255 
00256   if (message == NULL)
00257     message = message_from_error (name);
00258   
00259   real = (DBusRealError *)error;
00260   
00261   real->name = (char*) name;
00262   real->message = (char *)message;
00263   real->const_message = TRUE;
00264 }
00265 
00276 void
00277 dbus_move_error (DBusError *src,
00278                  DBusError *dest)
00279 {
00280   _dbus_return_if_error_is_set (dest);
00281 
00282   if (dest)
00283     {
00284       dbus_error_free (dest);
00285       *dest = *src;
00286       dbus_error_init (src);
00287     }
00288   else
00289     dbus_error_free (src);
00290 }
00291 
00299 dbus_bool_t
00300 dbus_error_has_name (const DBusError *error,
00301                      const char      *name)
00302 {
00303   _dbus_return_val_if_fail (error != NULL, FALSE);
00304   _dbus_return_val_if_fail (name != NULL, FALSE);
00305 
00306   _dbus_assert ((error->name != NULL && error->message != NULL) ||
00307                 (error->name == NULL && error->message == NULL));
00308   
00309   if (error->name != NULL)
00310     {
00311       DBusString str1, str2;
00312       _dbus_string_init_const (&str1, error->name);
00313       _dbus_string_init_const (&str2, name);
00314       return _dbus_string_equal (&str1, &str2);
00315     }
00316   else
00317     return FALSE;
00318 }
00319 
00326 dbus_bool_t
00327 dbus_error_is_set (const DBusError *error)
00328 {
00329   _dbus_return_val_if_fail (error != NULL, FALSE);  
00330   _dbus_assert ((error->name != NULL && error->message != NULL) ||
00331                 (error->name == NULL && error->message == NULL));
00332   return error->name != NULL;
00333 }
00334 
00351 void
00352 dbus_set_error (DBusError  *error,
00353                 const char *name,
00354                 const char *format,
00355                 ...)
00356 {
00357   DBusRealError *real;
00358   DBusString str;
00359   va_list args;
00360   
00361   if (error == NULL)
00362     return;
00363 
00364   /* it's a bug to pile up errors */
00365   _dbus_return_if_error_is_set (error);
00366   _dbus_return_if_fail (name != NULL);
00367   
00368   _dbus_assert (error->name == NULL);
00369   _dbus_assert (error->message == NULL);
00370 
00371   if (!_dbus_string_init (&str))
00372     goto nomem;
00373   
00374   if (format == NULL)
00375     {
00376       if (!_dbus_string_append (&str,
00377                                 message_from_error (name)))
00378         {
00379           _dbus_string_free (&str);
00380           va_end (args);
00381           goto nomem;
00382         }
00383     }
00384   else
00385     {
00386       va_start (args, format);
00387       if (!_dbus_string_append_printf_valist (&str, format, args))
00388         {
00389           _dbus_string_free (&str);
00390           va_end (args);
00391           goto nomem;
00392         }
00393       va_end (args);
00394     }
00395 
00396   real = (DBusRealError *)error;
00397 
00398   if (!_dbus_string_steal_data (&str, &real->message))
00399     {
00400       _dbus_string_free (&str);
00401       goto nomem;
00402     }
00403   _dbus_string_free (&str);
00404   
00405   real->name = _dbus_strdup (name);
00406   if (real->name == NULL)
00407     {
00408       dbus_free (real->message);
00409       real->message = NULL;
00410       goto nomem;
00411     }
00412   real->const_message = FALSE;
00413 
00414   return;
00415   
00416  nomem:
00417   _DBUS_SET_OOM (error);
00418 }
00419  /* End public API */

Generated on Tue Dec 21 2010 for D-Bus by  doxygen 1.7.1