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 {
00184 dbus_free (real->name);
00185 dbus_free (real->message);
00186 }
00187
00188 dbus_error_init (error);
00189 }
00190
00203 void
00204 dbus_set_error_const (DBusError *error,
00205 const char *name,
00206 const char *message)
00207 {
00208 DBusRealError *real;
00209
00210 _dbus_return_if_error_is_set (error);
00211 _dbus_return_if_fail (name != NULL);
00212
00213 if (error == NULL)
00214 return;
00215
00216 _dbus_assert (error->name == NULL);
00217 _dbus_assert (error->message == NULL);
00218
00219 if (message == NULL)
00220 message = message_from_error (name);
00221
00222 real = (DBusRealError *)error;
00223
00224 real->name = name;
00225 real->message = (char *)message;
00226 real->const_message = TRUE;
00227 }
00228
00239 void
00240 dbus_move_error (DBusError *src,
00241 DBusError *dest)
00242 {
00243 _dbus_return_if_error_is_set (dest);
00244
00245 if (dest)
00246 {
00247 dbus_error_free (dest);
00248 *dest = *src;
00249 dbus_error_init (src);
00250 }
00251 else
00252 dbus_error_free (src);
00253 }
00254
00262 dbus_bool_t
00263 dbus_error_has_name (const DBusError *error,
00264 const char *name)
00265 {
00266 _dbus_return_val_if_fail (error != NULL, FALSE);
00267 _dbus_return_val_if_fail (name != NULL, FALSE);
00268
00269 _dbus_assert ((error->name != NULL && error->message != NULL) ||
00270 (error->name == NULL && error->message == NULL));
00271
00272 if (error->name != NULL)
00273 {
00274 DBusString str1, str2;
00275 _dbus_string_init_const (&str1, error->name);
00276 _dbus_string_init_const (&str2, name);
00277 return _dbus_string_equal (&str1, &str2);
00278 }
00279 else
00280 return FALSE;
00281 }
00282
00289 dbus_bool_t
00290 dbus_error_is_set (const DBusError *error)
00291 {
00292 _dbus_return_val_if_fail (error != NULL, FALSE);
00293 _dbus_assert ((error->name != NULL && error->message != NULL) ||
00294 (error->name == NULL && error->message == NULL));
00295 return error->name != NULL;
00296 }
00297
00315 void
00316 dbus_set_error (DBusError *error,
00317 const char *name,
00318 const char *format,
00319 ...)
00320 {
00321 DBusRealError *real;
00322 DBusString str;
00323 va_list args;
00324
00325 if (error == NULL)
00326 return;
00327
00328
00329 _dbus_return_if_error_is_set (error);
00330 _dbus_return_if_fail (name != NULL);
00331
00332 _dbus_assert (error->name == NULL);
00333 _dbus_assert (error->message == NULL);
00334
00335 if (!_dbus_string_init (&str))
00336 goto nomem;
00337
00338 if (format == NULL)
00339 {
00340 if (!_dbus_string_append (&str,
00341 message_from_error (name)))
00342 {
00343 _dbus_string_free (&str);
00344 goto nomem;
00345 }
00346 }
00347 else
00348 {
00349 va_start (args, format);
00350 if (!_dbus_string_append_printf_valist (&str, format, args))
00351 {
00352 _dbus_string_free (&str);
00353 goto nomem;
00354 }
00355 va_end (args);
00356 }
00357
00358 real = (DBusRealError *)error;
00359
00360 if (!_dbus_string_steal_data (&str, &real->message))
00361 {
00362 _dbus_string_free (&str);
00363 goto nomem;
00364 }
00365 _dbus_string_free (&str);
00366
00367 real->name = _dbus_strdup (name);
00368 if (real->name == NULL)
00369 {
00370 dbus_free (real->message);
00371 real->message = NULL;
00372 goto nomem;
00373 }
00374 real->const_message = FALSE;
00375
00376 return;
00377
00378 nomem:
00379 _DBUS_SET_OOM (error);
00380 }
00381