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 <stdarg.h>
00028 #include <string.h>
00029
00040 typedef struct
00041 {
00042 const char *name;
00043 char *message;
00045 unsigned int const_message : 1;
00047 unsigned int dummy2 : 1;
00048 unsigned int dummy3 : 1;
00049 unsigned int dummy4 : 1;
00050 unsigned int dummy5 : 1;
00052 void *padding1;
00054 } DBusRealError;
00055
00064 static const char*
00065 message_from_error (const char *error)
00066 {
00067 if (strcmp (error, DBUS_ERROR_FAILED) == 0)
00068 return "Unknown error";
00069 else if (strcmp (error, DBUS_ERROR_NO_MEMORY) == 0)
00070 return "Not enough memory available";
00071 else if (strcmp (error, DBUS_ERROR_IO_ERROR) == 0)
00072 return "Error reading or writing data";
00073 else if (strcmp (error, DBUS_ERROR_BAD_ADDRESS) == 0)
00074 return "Could not parse address";
00075 else if (strcmp (error, DBUS_ERROR_NOT_SUPPORTED) == 0)
00076 return "Feature not supported";
00077 else if (strcmp (error, DBUS_ERROR_LIMITS_EXCEEDED) == 0)
00078 return "Resource limits exceeded";
00079 else if (strcmp (error, DBUS_ERROR_ACCESS_DENIED) == 0)
00080 return "Permission denied";
00081 else if (strcmp (error, DBUS_ERROR_AUTH_FAILED) == 0)
00082 return "Could not authenticate to server";
00083 else if (strcmp (error, DBUS_ERROR_NO_SERVER) == 0)
00084 return "No server available at address";
00085 else if (strcmp (error, DBUS_ERROR_TIMEOUT) == 0)
00086 return "Connection timed out";
00087 else if (strcmp (error, DBUS_ERROR_NO_NETWORK) == 0)
00088 return "Network unavailable";
00089 else if (strcmp (error, DBUS_ERROR_ADDRESS_IN_USE) == 0)
00090 return "Address already in use";
00091 else if (strcmp (error, DBUS_ERROR_DISCONNECTED) == 0)
00092 return "Disconnected.";
00093 else if (strcmp (error, DBUS_ERROR_INVALID_ARGS) == 0)
00094 return "Invalid arguments.";
00095 else if (strcmp (error, DBUS_ERROR_NO_REPLY) == 0)
00096 return "Did not get a reply message.";
00097 else if (strcmp (error, DBUS_ERROR_FILE_NOT_FOUND) == 0)
00098 return "File doesn't exist.";
00099 else
00100 return error;
00101 }
00102
00104
00149 void
00150 dbus_error_init (DBusError *error)
00151 {
00152 DBusRealError *real;
00153
00154 _dbus_return_if_fail (error != NULL);
00155
00156 _dbus_assert (sizeof (DBusError) == sizeof (DBusRealError));
00157
00158 real = (DBusRealError *)error;
00159
00160 real->name = NULL;
00161 real->message = NULL;
00162
00163 real->const_message = TRUE;
00164 }
00165
00172 void
00173 dbus_error_free (DBusError *error)
00174 {
00175 DBusRealError *real;
00176
00177 _dbus_return_if_fail (error != NULL);
00178
00179 real = (DBusRealError *)error;
00180
00181 if (!real->const_message)
00182 dbus_free (real->message);
00183
00184 dbus_error_init (error);
00185 }
00186
00199 void
00200 dbus_set_error_const (DBusError *error,
00201 const char *name,
00202 const char *message)
00203 {
00204 DBusRealError *real;
00205
00206 _dbus_return_if_error_is_set (error);
00207 _dbus_return_if_fail (name != NULL);
00208
00209 if (error == NULL)
00210 return;
00211
00212 _dbus_assert (error->name == NULL);
00213 _dbus_assert (error->message == NULL);
00214
00215 if (message == NULL)
00216 message = message_from_error (name);
00217
00218 real = (DBusRealError *)error;
00219
00220 real->name = name;
00221 real->message = (char *)message;
00222 real->const_message = TRUE;
00223 }
00224
00235 void
00236 dbus_move_error (DBusError *src,
00237 DBusError *dest)
00238 {
00239 _dbus_return_if_error_is_set (dest);
00240
00241 if (dest)
00242 {
00243 dbus_error_free (dest);
00244 *dest = *src;
00245 dbus_error_init (src);
00246 }
00247 else
00248 dbus_error_free (src);
00249 }
00250
00258 dbus_bool_t
00259 dbus_error_has_name (const DBusError *error,
00260 const char *name)
00261 {
00262 _dbus_return_val_if_fail (error != NULL, FALSE);
00263 _dbus_return_val_if_fail (name != NULL, FALSE);
00264
00265 _dbus_assert ((error->name != NULL && error->message != NULL) ||
00266 (error->name == NULL && error->message == NULL));
00267
00268 if (error->name != NULL)
00269 {
00270 DBusString str1, str2;
00271 _dbus_string_init_const (&str1, error->name);
00272 _dbus_string_init_const (&str2, name);
00273 return _dbus_string_equal (&str1, &str2);
00274 }
00275 else
00276 return FALSE;
00277 }
00278
00285 dbus_bool_t
00286 dbus_error_is_set (const DBusError *error)
00287 {
00288 _dbus_return_val_if_fail (error != NULL, FALSE);
00289 _dbus_assert ((error->name != NULL && error->message != NULL) ||
00290 (error->name == NULL && error->message == NULL));
00291 return error->name != NULL;
00292 }
00293
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
00362 real->name = name;
00363 real->const_message = FALSE;
00364
00365 _dbus_string_free (&str);
00366
00367 return;
00368
00369 nomem:
00370 dbus_set_error_const (error, DBUS_ERROR_NO_MEMORY, NULL);
00371 }
00372