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., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00022  *
00023  */
00024 #include "dbus-errors.h"
00025 #include "dbus-internals.h"
00026 #include "dbus-string.h"
00027 #include "dbus-protocol.h"
00028 #include <stdarg.h>
00029 #include <string.h>
00030 
00063 typedef struct
00064 {
00065   char *name; 
00066   char *message; 
00068   unsigned int const_message : 1; 
00070   unsigned int dummy2 : 1; 
00071   unsigned int dummy3 : 1; 
00072   unsigned int dummy4 : 1; 
00073   unsigned int dummy5 : 1; 
00075   void *padding1; 
00077 } DBusRealError;
00078 
00087 static const char*
00088 message_from_error (const char *error)
00089 {
00090   if (strcmp (error, DBUS_ERROR_FAILED) == 0)
00091     return "Unknown error";
00092   else if (strcmp (error, DBUS_ERROR_NO_MEMORY) == 0)
00093     return "Not enough memory available";
00094   else if (strcmp (error, DBUS_ERROR_IO_ERROR) == 0)
00095     return "Error reading or writing data";
00096   else if (strcmp (error, DBUS_ERROR_BAD_ADDRESS) == 0)
00097     return "Could not parse address";
00098   else if (strcmp (error, DBUS_ERROR_NOT_SUPPORTED) == 0)
00099     return "Feature not supported";
00100   else if (strcmp (error, DBUS_ERROR_LIMITS_EXCEEDED) == 0)
00101     return "Resource limits exceeded";
00102   else if (strcmp (error, DBUS_ERROR_ACCESS_DENIED) == 0)
00103     return "Permission denied";
00104   else if (strcmp (error, DBUS_ERROR_AUTH_FAILED) == 0)
00105     return "Could not authenticate to server";
00106   else if (strcmp (error, DBUS_ERROR_NO_SERVER) == 0)
00107     return "No server available at address";
00108   else if (strcmp (error, DBUS_ERROR_TIMEOUT) == 0)
00109     return "Connection timed out";
00110   else if (strcmp (error, DBUS_ERROR_NO_NETWORK) == 0)
00111     return "Network unavailable";
00112   else if (strcmp (error, DBUS_ERROR_ADDRESS_IN_USE) == 0)
00113     return "Address already in use";
00114   else if (strcmp (error, DBUS_ERROR_DISCONNECTED) == 0)
00115     return "Disconnected.";
00116   else if (strcmp (error, DBUS_ERROR_INVALID_ARGS) == 0)
00117     return "Invalid arguments.";
00118   else if (strcmp (error, DBUS_ERROR_NO_REPLY) == 0)
00119     return "Did not get a reply message.";
00120   else if (strcmp (error, DBUS_ERROR_FILE_NOT_FOUND) == 0)
00121     return "File doesn't exist.";
00122   else if (strcmp (error, DBUS_ERROR_OBJECT_PATH_IN_USE) == 0)
00123     return "Object path already in use";
00124   else
00125     return error;
00126 }
00127  /* End of internals */
00129 
00183 void
00184 dbus_error_init (DBusError *error)
00185 {
00186   DBusRealError *real;
00187 
00188   _dbus_return_if_fail (error != NULL);
00189 
00190   _dbus_assert (sizeof (DBusError) == sizeof (DBusRealError));
00191 
00192   real = (DBusRealError *)error;
00193   
00194   real->name = NULL;  
00195   real->message = NULL;
00196 
00197   real->const_message = TRUE;
00198 }
00199 
00206 void
00207 dbus_error_free (DBusError *error)
00208 {
00209   DBusRealError *real;
00210 
00211   _dbus_return_if_fail (error != NULL);
00212   
00213   real = (DBusRealError *)error;
00214 
00215   if (!real->const_message)
00216     {
00217       dbus_free (real->name);
00218       dbus_free (real->message);
00219     }
00220 
00221   dbus_error_init (error);
00222 }
00223 
00238 void
00239 dbus_set_error_const (DBusError  *error,
00240                       const char *name,
00241                       const char *message)
00242 {
00243   DBusRealError *real;
00244 
00245   _dbus_return_if_error_is_set (error);
00246   _dbus_return_if_fail (name != NULL);
00247   
00248   if (error == NULL)
00249     return;
00250 
00251   _dbus_assert (error->name == NULL);
00252   _dbus_assert (error->message == NULL);
00253 
00254   if (message == NULL)
00255     message = message_from_error (name);
00256   
00257   real = (DBusRealError *)error;
00258   
00259   real->name = (char*) name;
00260   real->message = (char *)message;
00261   real->const_message = TRUE;
00262 }
00263 
00274 void
00275 dbus_move_error (DBusError *src,
00276                  DBusError *dest)
00277 {
00278   _dbus_return_if_error_is_set (dest);
00279 
00280   if (dest)
00281     {
00282       dbus_error_free (dest);
00283       *dest = *src;
00284       dbus_error_init (src);
00285     }
00286   else
00287     dbus_error_free (src);
00288 }
00289 
00297 dbus_bool_t
00298 dbus_error_has_name (const DBusError *error,
00299                      const char      *name)
00300 {
00301   _dbus_return_val_if_fail (error != NULL, FALSE);
00302   _dbus_return_val_if_fail (name != NULL, FALSE);
00303 
00304   _dbus_assert ((error->name != NULL && error->message != NULL) ||
00305                 (error->name == NULL && error->message == NULL));
00306   
00307   if (error->name != NULL)
00308     {
00309       DBusString str1, str2;
00310       _dbus_string_init_const (&str1, error->name);
00311       _dbus_string_init_const (&str2, name);
00312       return _dbus_string_equal (&str1, &str2);
00313     }
00314   else
00315     return FALSE;
00316 }
00317 
00324 dbus_bool_t
00325 dbus_error_is_set (const DBusError *error)
00326 {
00327   _dbus_return_val_if_fail (error != NULL, FALSE);  
00328   _dbus_assert ((error->name != NULL && error->message != NULL) ||
00329                 (error->name == NULL && error->message == NULL));
00330   return error->name != NULL;
00331 }
00332 
00349 void
00350 dbus_set_error (DBusError  *error,
00351                 const char *name,
00352                 const char *format,
00353                 ...)
00354 {
00355   DBusRealError *real;
00356   DBusString str;
00357   va_list args;
00358   
00359   if (error == NULL)
00360     return;
00361 
00362   /* it's a bug to pile up errors */
00363   _dbus_return_if_error_is_set (error);
00364   _dbus_return_if_fail (name != NULL);
00365   
00366   _dbus_assert (error->name == NULL);
00367   _dbus_assert (error->message == NULL);
00368 
00369   if (!_dbus_string_init (&str))
00370     goto nomem;
00371   
00372   if (format == NULL)
00373     {
00374       if (!_dbus_string_append (&str,
00375                                 message_from_error (name)))
00376         {
00377           _dbus_string_free (&str);
00378           goto nomem;
00379         }
00380     }
00381   else
00382     {
00383       va_start (args, format);
00384       if (!_dbus_string_append_printf_valist (&str, format, args))
00385         {
00386           _dbus_string_free (&str);
00387           va_end (args);
00388           goto nomem;
00389         }
00390       va_end (args);
00391     }
00392 
00393   real = (DBusRealError *)error;
00394 
00395   if (!_dbus_string_steal_data (&str, &real->message))
00396     {
00397       _dbus_string_free (&str);
00398       goto nomem;
00399     }
00400   _dbus_string_free (&str);
00401   
00402   real->name = _dbus_strdup (name);
00403   if (real->name == NULL)
00404     {
00405       dbus_free (real->message);
00406       real->message = NULL;
00407       goto nomem;
00408     }
00409   real->const_message = FALSE;
00410 
00411   return;
00412   
00413  nomem:
00414   _DBUS_SET_OOM (error);
00415 }
00416  /* End public API */

Generated on Sat Dec 6 19:43:43 2008 for D-Bus by  doxygen 1.5.1