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 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
00159 void
00160 dbus_error_init (DBusError *error)
00161 {
00162 DBusRealError *real;
00163
00164 _dbus_return_if_fail (error != NULL);
00165
00166 _dbus_assert (sizeof (DBusError) == sizeof (DBusRealError));
00167
00168 real = (DBusRealError *)error;
00169
00170 real->name = NULL;
00171 real->message = NULL;
00172
00173 real->const_message = TRUE;
00174 }
00175
00182 void
00183 dbus_error_free (DBusError *error)
00184 {
00185 DBusRealError *real;
00186
00187 _dbus_return_if_fail (error != NULL);
00188
00189 real = (DBusRealError *)error;
00190
00191 if (!real->const_message)
00192 {
00193 dbus_free (real->name);
00194 dbus_free (real->message);
00195 }
00196
00197 dbus_error_init (error);
00198 }
00199
00214 void
00215 dbus_set_error_const (DBusError *error,
00216 const char *name,
00217 const char *message)
00218 {
00219 DBusRealError *real;
00220
00221 _dbus_return_if_error_is_set (error);
00222 _dbus_return_if_fail (name != NULL);
00223
00224 if (error == NULL)
00225 return;
00226
00227 _dbus_assert (error->name == NULL);
00228 _dbus_assert (error->message == NULL);
00229
00230 if (message == NULL)
00231 message = message_from_error (name);
00232
00233 real = (DBusRealError *)error;
00234
00235 real->name = (char*) name;
00236 real->message = (char *)message;
00237 real->const_message = TRUE;
00238 }
00239
00250 void
00251 dbus_move_error (DBusError *src,
00252 DBusError *dest)
00253 {
00254 _dbus_return_if_error_is_set (dest);
00255
00256 if (dest)
00257 {
00258 dbus_error_free (dest);
00259 *dest = *src;
00260 dbus_error_init (src);
00261 }
00262 else
00263 dbus_error_free (src);
00264 }
00265
00273 dbus_bool_t
00274 dbus_error_has_name (const DBusError *error,
00275 const char *name)
00276 {
00277 _dbus_return_val_if_fail (error != NULL, FALSE);
00278 _dbus_return_val_if_fail (name != NULL, FALSE);
00279
00280 _dbus_assert ((error->name != NULL && error->message != NULL) ||
00281 (error->name == NULL && error->message == NULL));
00282
00283 if (error->name != NULL)
00284 {
00285 DBusString str1, str2;
00286 _dbus_string_init_const (&str1, error->name);
00287 _dbus_string_init_const (&str2, name);
00288 return _dbus_string_equal (&str1, &str2);
00289 }
00290 else
00291 return FALSE;
00292 }
00293
00300 dbus_bool_t
00301 dbus_error_is_set (const DBusError *error)
00302 {
00303 _dbus_return_val_if_fail (error != NULL, FALSE);
00304 _dbus_assert ((error->name != NULL && error->message != NULL) ||
00305 (error->name == NULL && error->message == NULL));
00306 return error->name != NULL;
00307 }
00308
00325 void
00326 dbus_set_error (DBusError *error,
00327 const char *name,
00328 const char *format,
00329 ...)
00330 {
00331 DBusRealError *real;
00332 DBusString str;
00333 va_list args;
00334
00335 if (error == NULL)
00336 return;
00337
00338
00339 _dbus_return_if_error_is_set (error);
00340 _dbus_return_if_fail (name != NULL);
00341
00342 _dbus_assert (error->name == NULL);
00343 _dbus_assert (error->message == NULL);
00344
00345 if (!_dbus_string_init (&str))
00346 goto nomem;
00347
00348 if (format == NULL)
00349 {
00350 if (!_dbus_string_append (&str,
00351 message_from_error (name)))
00352 {
00353 _dbus_string_free (&str);
00354 goto nomem;
00355 }
00356 }
00357 else
00358 {
00359 va_start (args, format);
00360 if (!_dbus_string_append_printf_valist (&str, format, args))
00361 {
00362 _dbus_string_free (&str);
00363 goto nomem;
00364 }
00365 va_end (args);
00366 }
00367
00368 real = (DBusRealError *)error;
00369
00370 if (!_dbus_string_steal_data (&str, &real->message))
00371 {
00372 _dbus_string_free (&str);
00373 goto nomem;
00374 }
00375 _dbus_string_free (&str);
00376
00377 real->name = _dbus_strdup (name);
00378 if (real->name == NULL)
00379 {
00380 dbus_free (real->message);
00381 real->message = NULL;
00382 goto nomem;
00383 }
00384 real->const_message = FALSE;
00385
00386 return;
00387
00388 nomem:
00389 _DBUS_SET_OOM (error);
00390 }
00391