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