2 * Copyright (c) 2007-2012 Zmanda, Inc. All Rights Reserved.
4 * This program is free software; you can redistribute it and/or modify it
5 * under the terms of the GNU General Public License version 2 as published
6 * by the Free Software Foundation.
8 * This program is distributed in the hope that it will be useful, but
9 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
10 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
13 * You should have received a copy of the GNU General Public License along
14 * with this program; if not, write to the Free Software Foundation, Inc.,
15 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 * Contact information: Zmanda Inc., 465 S. Mathilda Ave., Suite 300
18 * Sunnyvale, CA 94085, USA, or: http://www.zmanda.com
22 * Utilities that aren't quite included in glib
24 * Author: Dustin J. Mitchell <dustin@zmanda.com>, Ian Turner <ian@zmanda.com>
28 #include "glib-util.h"
29 #include "conffile.h" /* For find_multiplier. */
32 #include <curl/curl.h>
34 #ifdef LIBCURL_USE_OPENSSL
35 #include <openssl/crypto.h>
36 static GMutex **openssl_mutex_array;
37 static void openssl_lock_callback(int mode, int type, const char *file, int line)
41 if (mode & CRYPTO_LOCK) {
42 g_mutex_lock(openssl_mutex_array[type]);
45 g_mutex_unlock(openssl_mutex_array[type]);
54 openssl_mutex_array = g_new0(GMutex *, CRYPTO_num_locks());
56 for (i=0; i<CRYPTO_num_locks(); i++) {
57 openssl_mutex_array[i] = g_mutex_new();
59 CRYPTO_set_locking_callback(openssl_lock_callback);
63 #else /* LIBCURL_USE_OPENSSL */
64 #if defined LIBCURL_USE_GNUTLS
69 GCRY_THREAD_OPTION_PTHREAD_IMPL;
73 gcry_control(GCRYCTL_SET_THREAD_CBS);
76 #else /* LIBCURL_USE_GNUTLS */
82 #endif /* LIBCURL_USE_GNUTLS */
83 #endif /* LIBCURL_USE_OPENSSL */
85 #else /* HAVE_LIBCURL */
90 #endif /* HAVE_LIBCURL */
94 static gboolean did_glib_init = FALSE;
95 if (did_glib_init) return;
98 /* set up libcurl (this must happen before threading
101 # ifdef G_THREADS_ENABLED
102 # if (GLIB_MAJOR_VERSION < 2 || (GLIB_MAJOR_VERSION == 2 && GLIB_MINOR_VERSION < 31))
103 g_assert(!g_thread_supported()); /* assert threads aren't initialized yet */
106 g_assert(curl_global_init(CURL_GLOBAL_ALL) == 0);
109 /* do a version check */
110 #if GLIB_CHECK_VERSION(2,6,0)
112 const char *glib_err = glib_check_version(GLIB_MAJOR_VERSION,
116 error(_("%s: Amanda was compiled with glib-%d.%d.%d"), glib_err,
117 GLIB_MAJOR_VERSION, GLIB_MINOR_VERSION, GLIB_MICRO_VERSION);
118 exit(1); /* glib_init may be called before error handling is set up */
123 /* Initialize glib's type system. On glib >= 2.24, this will initialize
124 * threads, so it must be done after curl is initialized. */
127 /* And set up glib's threads */
128 #if defined(G_THREADS_ENABLED) && !defined(G_THREADS_IMPL_NONE)
129 if (!g_thread_supported())
140 FLAG_STRING_SHORT_NAME,
144 static char ** g_flags_to_strv(int value, GType type, FlagString source);
147 _glib_util_foreach_glue(gpointer data, gpointer func)
149 void (*one_arg_fn)(gpointer) = (void (*)(gpointer))func;
153 GValue* g_value_unset_init(GValue* value, GType type) {
154 g_return_val_if_fail(value != NULL, NULL);
156 if (G_IS_VALUE(value)) {
157 g_value_unset(value);
159 g_value_init(value, type);
163 GValue* g_value_unset_copy(const GValue * from, GValue * to) {
164 g_return_val_if_fail(from != NULL, NULL);
165 g_return_val_if_fail(to != NULL, NULL);
167 g_value_unset_init(to, G_VALUE_TYPE(from));
168 g_value_copy(from, to);
172 #if (GLIB_MAJOR_VERSION < 2 || (GLIB_MAJOR_VERSION == 2 && GLIB_MINOR_VERSION < 28))
173 void slist_free_full(GSList * list, GDestroyNotify free_fn) {
176 while (cur != NULL) {
177 gpointer data = cur->data;
179 cur = g_slist_next(cur);
186 void g_ptr_array_free_full(GPtrArray * array) {
189 for (i = 0; i < array->len; i ++) {
190 amfree(g_ptr_array_index(array, i));
192 g_ptr_array_free(array, TRUE);
195 gboolean g_value_compare(GValue * a, GValue * b) {
196 if (a == NULL && b == NULL)
198 if (a == NULL || b == NULL)
200 if (G_VALUE_TYPE(a) != G_VALUE_TYPE(b))
202 if (g_value_fits_pointer(a) && g_value_fits_pointer(b)) {
203 return g_value_peek_pointer(a) == g_value_peek_pointer(b);
205 /* Since there is no builtin comparison function, we resort to
206 comparing serialized strings. Yuck. */
210 a_str = g_strdup_value_contents(a);
211 b_str = g_strdup_value_contents(b);
212 rval = (0 == strcmp(a_str, b_str));
218 g_assert_not_reached();
222 g_value_set_boolean_from_string(
226 int b = string_to_boolean(str);
231 g_value_set_boolean(val, b);
235 static gboolean g_value_set_int_from_string(GValue * val, char * string) {
236 long int strto_result;
239 strto_result = strtol(string, &strto_end, 0);
240 multiplier = find_multiplier(strto_end);
241 if (multiplier == G_MAXINT64) {
242 if (strto_result >= 0) {
243 g_value_set_int(val, G_MAXINT);
245 g_value_set_int(val, G_MININT);
248 } else if (*string == '\0' || multiplier == 0
249 || strto_result < G_MININT / multiplier
250 || strto_result > G_MAXINT / multiplier) {
253 g_value_set_int(val, (int)(strto_result * multiplier));
258 static gboolean g_value_set_uint_from_string(GValue * val, char * string) {
259 unsigned long int strto_result;
262 strto_result = strtoul(string, &strto_end, 0);
263 multiplier = find_multiplier(strto_end); /* casts */
264 if (multiplier == G_MAXINT64) {
265 g_value_set_uint(val, G_MAXUINT);
267 } else if (multiplier == 0 || *string == '\0' ||
268 strto_result > G_MAXUINT / multiplier) {
271 g_value_set_uint(val, (guint)(strto_result * multiplier));
276 static gboolean g_value_set_uint64_from_string(GValue * val, char * string) {
277 unsigned long long int strto_result;
280 strto_result = strtoull(string, &strto_end, 0);
281 multiplier = find_multiplier(strto_end); /* casts */
282 if (multiplier == G_MAXINT64) {
283 g_value_set_uint64(val, G_MAXUINT64);
285 } else if (multiplier == 0 || *string == '\0' ||
286 strto_result > G_MAXUINT64 / multiplier) {
289 g_value_set_uint64(val, (guint64)(strto_result * multiplier));
294 /* Flags can contain multiple values. We assume here that values are like
295 * C identifiers (that is, they match /[A-Za-z_][A-Za-z0-9_]+/), although
296 * that doesn't seem to be a requirement of GLib. With that assumption in
297 * mind, we look for the format "FLAG_1 | FLAG_2 | ... | FLAG_N". */
298 static gboolean g_value_set_flags_from_string(GValue * val, char * string) {
300 char * strtok_saveptr;
302 char * strtok_first_arg;
303 const char delim[] = " \t,|";
304 GFlagsClass * flags_class;
306 flags_class = (GFlagsClass*) g_type_class_ref(G_VALUE_TYPE(val));
307 g_return_val_if_fail(flags_class != NULL, FALSE);
308 g_return_val_if_fail(G_IS_FLAGS_CLASS(flags_class), FALSE);
310 /* Don't let strtok stop on original. */
311 strtok_first_arg = string_copy = strdup(string);
314 GFlagsValue * flag_value;
315 char * token = strtok_r(strtok_first_arg, delim, &strtok_saveptr);
316 strtok_first_arg = NULL;
322 flag_value = g_flags_get_value_by_name(flags_class, token);
323 if (flag_value == NULL) {
324 flag_value = g_flags_get_value_by_nick(flags_class, token);
326 if (flag_value == NULL) {
327 g_fprintf(stderr, _("Invalid flag %s for type %s\n"), token,
328 g_type_name(G_VALUE_TYPE(val)));
332 value |= flag_value->value;
338 g_fprintf(stderr, _("No valid flags for type %s in string %s\n"),
339 g_type_name(G_VALUE_TYPE(val)), string);
343 g_value_set_flags(val, value);
348 /* This function really ought not to be part of Amanda. In my (Ian's) opinion,
349 serialization and deserialization should be a part of the GValue
350 interface. But it's not, and here we are. */
351 gboolean g_value_set_from_string(GValue * val, char * string) {
352 g_return_val_if_fail(val != NULL, FALSE);
353 g_return_val_if_fail(G_IS_VALUE(val), FALSE);
355 if (G_VALUE_HOLDS_BOOLEAN(val)) {
356 return g_value_set_boolean_from_string(val, string);
357 } else if (G_VALUE_HOLDS_INT(val)) {
358 return g_value_set_int_from_string(val, string);
359 } else if (G_VALUE_HOLDS_UINT(val)) {
360 return g_value_set_uint_from_string(val, string);
361 } else if (G_VALUE_HOLDS_UINT64(val)) {
362 return g_value_set_uint64_from_string(val, string);
363 } else if (G_VALUE_HOLDS_STRING(val)) {
364 g_value_set_string(val, string);
366 } else if (G_VALUE_HOLDS_FLAGS(val)) {
367 return g_value_set_flags_from_string(val, string);
378 return strcmp((char *)a, (char *)b);
381 char * g_strjoinv_and_free(char ** strv, const char * seperator) {
382 char * rval = g_strjoinv(seperator, strv);
387 char ** g_flags_name_to_strv(int value, GType type) {
388 return g_flags_to_strv(value, type, FLAG_STRING_NAME);
391 char ** g_flags_short_name_to_strv(int value, GType type) {
392 return g_flags_to_strv(value, type, FLAG_STRING_SHORT_NAME);
395 char ** g_flags_nick_to_strv(int value, GType type) {
396 return g_flags_to_strv(value, type, FLAG_STRING_NICK);
399 static char * get_name_from_value(GFlagsValue * value, FlagString source) {
401 case FLAG_STRING_NAME:
402 case FLAG_STRING_SHORT_NAME:
403 return strdup(value->value_name);
404 case FLAG_STRING_NICK:
405 return strdup(value->value_nick);
411 /* If freed and notfreed have a common prefix that is different from freed,
412 then return that and free freed. Otherwise, return freed. */
413 static char * find_common_prefix(char * freed, const char * notfreed) {
414 char * freed_ptr = freed;
415 const char * notfreed_ptr = notfreed;
418 if (notfreed == NULL) {
421 return strdup(notfreed);
423 } else if (notfreed == NULL) {
428 while (*freed_ptr == *notfreed_ptr) {
437 static char ** g_flags_to_strv(int value, GType type,
440 GFlagsValue * flagsvalue;
441 char * common_prefix = NULL;
442 int common_prefix_len;
445 g_return_val_if_fail(G_TYPE_IS_FLAGS(type), NULL);
446 g_return_val_if_fail((class = g_type_class_ref(type)) != NULL, NULL);
447 g_return_val_if_fail(G_IS_FLAGS_CLASS(class), NULL);
449 rval = g_ptr_array_new();
450 for (flagsvalue = class->values;
451 flagsvalue->value_name != NULL;
453 if (source == FLAG_STRING_SHORT_NAME) {
454 common_prefix = find_common_prefix(common_prefix,
455 flagsvalue->value_name);
458 if ((flagsvalue->value == 0 && value == 0) ||
459 (flagsvalue->value != 0 && (value & flagsvalue->value))) {
460 g_ptr_array_add(rval, get_name_from_value(flagsvalue, source));
464 if (source == FLAG_STRING_SHORT_NAME && common_prefix != NULL &&
465 ((common_prefix_len = strlen(common_prefix))) > 0) {
469 for (i = 0; i < rval->len; i ++) {
470 old = g_ptr_array_index(rval, i);
471 new = strdup(old + common_prefix_len);
472 g_ptr_array_index(rval, i) = new;
477 g_ptr_array_add(rval, NULL);
479 amfree(common_prefix);
480 return (char**)g_ptr_array_free(rval, FALSE);
483 char * g_english_strjoinv(char ** strv, const char * conjunction) {
488 strv = g_strdupv(strv);
490 length = g_strv_length(strv);
493 return stralloc(strv[0]);
495 last = strv[length - 1];
496 strv[length - 1] = NULL;
498 joined = g_strjoinv(", ", strv);
499 rval = g_strdup_printf("%s, %s %s", joined, conjunction, last);
507 char * g_english_strjoinv_and_free(char ** strv, const char * conjunction) {
508 char * rval = g_english_strjoinv(strv, conjunction);
513 #if !(GLIB_CHECK_VERSION(2,6,0))
514 guint g_strv_length(gchar ** strv) {
517 if (G_UNLIKELY(strv == NULL))
520 while (*strv != NULL) {
526 #endif /* GLIB_CHECK_VERSION(2.6.0) */
528 #if !GLIB_CHECK_VERSION(2,4,0)
530 g_ptr_array_foreach (GPtrArray *array,
536 g_return_if_fail (array);
538 for (i = 0; i < array->len; i++)
539 (*func) (array->pdata[i], user_data);
547 /* modified version of glib's hash function, copyright
548 * GLib Team and others 1997-2000. */
552 for (p = key; *p != '\0'; p++)
553 h = (h << 5) - h + (('_' == *p) ? '-' : g_ascii_tolower(*p));
563 const gchar *p1 = v1, *p2 = v2;
565 /* letting '-' == '_' */
566 if (!('-' == *p1 || '_' == *p1) || !('-' == *p2 || '_' == *p2))
567 if (g_ascii_tolower(*p1) != g_ascii_tolower(*p2))
574 /* p1 is at '\0' is p2 too? */
575 return *p2? FALSE : TRUE;