00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
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
00041 typedef struct
00042 {
00043 const char *name;
00044 char *message;
00046 unsigned int const_message : 1;
00048 unsigned int dummy2 : 1;
00049 unsigned int dummy3 : 1;
00050 unsigned int dummy4 : 1;
00051 unsigned int dummy5 : 1;
00053 void *padding1;
00055 } DBusRealError;
00056
00065 static const char*
00066 message_from_error (const char *error)
00067 {
00068 if (strcmp (error, DBUS_ERROR_FAILED) == 0)
00069 return "Unknown error";
00070 else if (strcmp (error, DBUS_ERROR_NO_MEMORY) == 0)
00071 return "Not enough memory available";
00072 else if (strcmp (error, DBUS_ERROR_IO_ERROR) == 0)
00073 return "Error reading or writing data";
00074 else if (strcmp (error, DBUS_ERROR_BAD_ADDRESS) == 0)
00075 return "Could not parse address";
00076 else if (strcmp (error, DBUS_ERROR_NOT_SUPPORTED) == 0)
00077 return "Feature not supported";
00078 else if (strcmp (error, DBUS_ERROR_LIMITS_EXCEEDED) == 0)
00079 return "Resource limits exceeded";
00080 else if (strcmp (error, DBUS_ERROR_ACCESS_DENIED) == 0)
00081 return "Permission denied";
00082 else if (strcmp (error, DBUS_ERROR_AUTH_FAILED) == 0)
00083 return "Could not authenticate to server";
00084 else if (strcmp (error, DBUS_ERROR_NO_SERVER) == 0)
00085 return "No server available at address";
00086 else if (strcmp (error, DBUS_ERROR_TIMEOUT) == 0)
00087 return "Connection timed out";
00088 else if (strcmp (error, DBUS_ERROR_NO_NETWORK) == 0)
00089 return "Network unavailable";
00090 else if (strcmp (error, DBUS_ERROR_ADDRESS_IN_USE) == 0)
00091 return "Address already in use";
00092 else if (strcmp (error, DBUS_ERROR_DISCONNECTED) == 0)
00093 return "Disconnected.";
00094 else if (strcmp (error, DBUS_ERROR_INVALID_ARGS) == 0)
00095 return "Invalid arguments.";
00096 else if (strcmp (error, DBUS_ERROR_NO_REPLY) == 0)
00097 return "Did not get a reply message.";
00098 else if (strcmp (error, DBUS_ERROR_FILE_NOT_FOUND) == 0)
00099 return "File doesn't exist.";
00100 else
00101 return error;
00102 }
00103
00105
00150 void
00151 dbus_error_init (DBusError *error)
00152 {
00153 DBusRealError *real;
00154
00155 _dbus_return_if_fail (error != NULL);
00156
00157 _dbus_assert (sizeof (DBusError) == sizeof (DBusRealError));
00158
00159 real = (DBusRealError *)error;
00160
00161 real->name = NULL;
00162 real->message = NULL;
00163
00164 real->const_message = TRUE;
00165 }
00166
00173 void
00174 dbus_error_free (DBusError *error)
00175 {
00176 DBusRealError *real;
00177
00178 _dbus_return_if_fail (error != NULL);
00179
00180 real = (DBusRealError *)error;
00181
00182 if (!real->const_message)
00183 dbus_free (real->message);
00184
00185 dbus_error_init (error);
00186 }
00187
00200 void
00201 dbus_set_error_const (DBusError *error,
00202 const char *name,
00203 const char *message)
00204 {
00205 DBusRealError *real;
00206
00207 _dbus_return_if_error_is_set (error);
00208 _dbus_return_if_fail (name != NULL);
00209
00210 if (error == NULL)
00211 return;
00212
00213 _dbus_assert (error->name == NULL);
00214 _dbus_assert (error->message == NULL);
00215
00216 if (message == NULL)
00217 message = message_from_error (name);
00218
00219 real = (DBusRealError *)error;
00220
00221 real->name = name;
00222 real->message = (char *)message;
00223 real->const_message = TRUE;
00224 }
00225
00236 void
00237 dbus_move_error (DBusError *src,
00238 DBusError *dest)
00239 {
00240 _dbus_return_if_error_is_set (dest);
00241
00242 if (dest)
00243 {
00244 dbus_error_free (dest);
00245 *dest = *src;
00246 dbus_error_init (src);
00247 }
00248 else
00249 dbus_error_free (src);
00250 }
00251
00259 dbus_bool_t
00260 dbus_error_has_name (const DBusError *error,
00261 const char *name)
00262 {
00263 _dbus_return_val_if_fail (error != NULL, FALSE);
00264 _dbus_return_val_if_fail (name != NULL, FALSE);
00265
00266 _dbus_assert ((error->name != NULL && error->message != NULL) ||
00267 (error->name == NULL && error->message == NULL));
00268
00269 if (error->name != NULL)
00270 {
00271 DBusString str1, str2;
00272 _dbus_string_init_const (&str1, error->name);
00273 _dbus_string_init_const (&str2, name);
00274 return _dbus_string_equal (&str1, &str2);
00275 }
00276 else
00277 return FALSE;
00278 }
00279
00286 dbus_bool_t
00287 dbus_error_is_set (const DBusError *error)
00288 {
00289 _dbus_return_val_if_fail (error != NULL, FALSE);
00290 _dbus_assert ((error->name != NULL && error->message != NULL) ||
00291 (error->name == NULL && error->message == NULL));
00292 return error->name != NULL;
00293 }
00294
00312 void
00313 dbus_set_error (DBusError *error,
00314 const char *name,
00315 const char *format,
00316 ...)
00317 {
00318 DBusRealError *real;
00319 DBusString str;
00320 va_list args;
00321
00322 if (error == NULL)
00323 return;
00324
00325
00326 _dbus_return_if_error_is_set (error);
00327 _dbus_return_if_fail (name != NULL);
00328
00329 _dbus_assert (error->name == NULL);
00330 _dbus_assert (error->message == NULL);
00331
00332 if (!_dbus_string_init (&str))
00333 goto nomem;
00334
00335 if (format == NULL)
00336 {
00337 if (!_dbus_string_append (&str,
00338 message_from_error (name)))
00339 {
00340 _dbus_string_free (&str);
00341 goto nomem;
00342 }
00343 }
00344 else
00345 {
00346 va_start (args, format);
00347 if (!_dbus_string_append_printf_valist (&str, format, args))
00348 {
00349 _dbus_string_free (&str);
00350 goto nomem;
00351 }
00352 va_end (args);
00353 }
00354
00355 real = (DBusRealError *)error;
00356
00357 if (!_dbus_string_steal_data (&str, &real->message))
00358 {
00359 _dbus_string_free (&str);
00360 goto nomem;
00361 }
00362
00363 real->name = name;
00364 real->const_message = FALSE;
00365
00366 _dbus_string_free (&str);
00367
00368 return;
00369
00370 nomem:
00371 _DBUS_SET_OOM (error);
00372 }
00373