this patch also appears to no longer be relevant
[debian/amanda] / common-src / glib-util.c
index ec9728ad4c8a24d7ad2a61f52bb3b0a789a8a007..a20d7459d0992af0b5db590a8906b167c32f13c0 100644 (file)
@@ -1,20 +1,20 @@
 /*
- * Copyright (c) 2005 Zmanda, Inc.  All Rights Reserved.
- * 
- * This library is free software; you can redistribute it and/or modify it
- * under the terms of the GNU Lesser General Public License version 2.1 as 
- * published by the Free Software Foundation.
- * 
- * This library is distributed in the hope that it will be useful, but
+ * Copyright (c) 2007-2012 Zmanda, Inc.  All Rights Reserved.
+ *
+ * This program is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License version 2 as published
+ * by the Free Software Foundation.
+ *
+ * This program is distributed in the hope that it will be useful, but
  * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
- * or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public
- * License for more details.
- * 
- * You should have received a copy of the GNU Lesser General Public License
- * along with this library; if not, write to the Free Software Foundation,
- * Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA.
- * 
- * Contact information: Zmanda Inc., 505 N Mathlida Ave, Suite 120
+ * or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
+ * for more details.
+ *
+ * You should have received a copy of the GNU General Public License along
+ * with this program; if not, write to the Free Software Foundation, Inc.,
+ * 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
+ *
+ * Contact information: Zmanda Inc., 465 S. Mathilda Ave., Suite 300
  * Sunnyvale, CA 94085, USA, or: http://www.zmanda.com
  */
 
 #include "glib-util.h"
 #include "conffile.h" /* For find_multiplier. */
 
+#ifdef HAVE_LIBCURL
+#include <curl/curl.h>
+
+#ifdef LIBCURL_USE_OPENSSL
+#include <openssl/crypto.h>
+static GMutex **openssl_mutex_array;
+static void openssl_lock_callback(int mode, int type, const char *file, int line)
+{
+    (void)file;
+    (void)line;
+    if (mode & CRYPTO_LOCK) {
+       g_mutex_lock(openssl_mutex_array[type]);
+    }
+    else {
+       g_mutex_unlock(openssl_mutex_array[type]);
+    }
+}
+
+static void
+init_ssl(void)
+{
+    int i;
+
+    openssl_mutex_array = g_new0(GMutex *, CRYPTO_num_locks());
+
+    for (i=0; i<CRYPTO_num_locks(); i++) {
+       openssl_mutex_array[i] = g_mutex_new();
+    }
+    CRYPTO_set_locking_callback(openssl_lock_callback);
+
+}
+
+#else /* LIBCURL_USE_OPENSSL */
+#if defined LIBCURL_USE_GNUTLS
+
+#include <gcrypt.h>
+#include <errno.h>
+
+GCRY_THREAD_OPTION_PTHREAD_IMPL;
+static void
+init_ssl(void)
+{
+  gcry_control(GCRYCTL_SET_THREAD_CBS);
+}
+
+#else  /* LIBCURL_USE_GNUTLS  */
+
+static void
+init_ssl(void)
+{
+}
+#endif /* LIBCURL_USE_GNUTLS  */
+#endif  /* LIBCURL_USE_OPENSSL */
+
+#else  /* HAVE_LIBCURL */
+static void
+init_ssl(void)
+{
+}
+#endif /* HAVE_LIBCURL */
+
+void
+glib_init(void) {
+    static gboolean did_glib_init = FALSE;
+    if (did_glib_init) return;
+    did_glib_init = TRUE;
+
+    /* set up libcurl */
+#ifdef HAVE_LIBCURL
+# ifdef G_THREADS_ENABLED
+#  if (GLIB_MAJOR_VERSION < 2 || (GLIB_MAJOR_VERSION == 2 && GLIB_MINOR_VERSION < 31))
+    g_assert(!g_thread_supported()); /* assert threads aren't initialized yet */
+#  endif
+# endif
+    g_assert(curl_global_init(CURL_GLOBAL_ALL) == 0);
+#endif
+
+    /* do a version check */
+#if GLIB_CHECK_VERSION(2,6,0)
+    {
+       const char *glib_err = glib_check_version(GLIB_MAJOR_VERSION,
+                                                 GLIB_MINOR_VERSION,
+                                                 GLIB_MICRO_VERSION);
+       if (glib_err) {
+           error(_("%s: Amanda was compiled with glib-%d.%d.%d"), glib_err,
+                   GLIB_MAJOR_VERSION, GLIB_MINOR_VERSION, GLIB_MICRO_VERSION);
+           exit(1); /* glib_init may be called before error handling is set up */
+       }
+    }
+#endif
+
+    /* Initialize glib's type system.  On glib >= 2.24, this will initialize
+     * threads, so it must be done after curl is initialized. */
+    g_type_init();
+
+    /* And set up glib's threads */
+#if defined(G_THREADS_ENABLED) && !defined(G_THREADS_IMPL_NONE)
+    if (!g_thread_supported())
+       g_thread_init(NULL);
+#endif
+
+    /* initialize ssl */
+    init_ssl();
+
+}
+
 typedef enum {
     FLAG_STRING_NAME,
     FLAG_STRING_SHORT_NAME,
@@ -62,38 +168,19 @@ GValue* g_value_unset_copy(const GValue * from, GValue * to) {
     return to;
 }
 
-void g_list_free_full(GList * list) {
-    GList * cur = list;
-
-    while (cur != NULL) {
-        gpointer data = cur->data;
-        amfree(data);
-        cur = g_list_next(cur);
-    }
-
-    g_list_free(list);
-}
-
-void g_slist_free_full(GSList * list) {
+#if (GLIB_MAJOR_VERSION < 2 || (GLIB_MAJOR_VERSION == 2 && GLIB_MINOR_VERSION < 28))
+void slist_free_full(GSList * list, GDestroyNotify free_fn) {
     GSList * cur = list;
 
     while (cur != NULL) {
         gpointer data = cur->data;
-        amfree(data);
+        free_fn(data);
         cur = g_slist_next(cur);
     }
 
     g_slist_free(list);
 }
-
-void g_queue_free_full(GQueue * queue) {
-    while (!g_queue_is_empty(queue)) {
-        gpointer data;
-        data = g_queue_pop_head(queue);
-        amfree(data);
-    }
-    g_queue_free(queue);
-}
+#endif
 
 void g_ptr_array_free_full(GPtrArray * array) {
     size_t i;
@@ -130,19 +217,17 @@ gboolean g_value_compare(GValue * a, GValue * b) {
     g_assert_not_reached();
 }
 
-static gboolean g_value_set_boolean_from_string(GValue * val, char * string) {
-    if (strcasecmp(string, "true") == 0 ||
-        strcasecmp(string, "yes") == 0 ||
-        strcmp(string, "1") == 0) {
-        g_value_set_boolean(val, TRUE);
-    } else if (strcasecmp(string, "false") == 0 ||
-               strcasecmp(string, "no") == 0 ||
-               strcmp(string, "0") == 0) {
-        g_value_set_boolean(val, FALSE);
-    } else {
-        return FALSE;
-    }
+static gboolean
+g_value_set_boolean_from_string(
+    GValue * val,
+    char * str)
+{
+    int b = string_to_boolean(str);
+
+    if (b == -1)
+       return FALSE;
 
+    g_value_set_boolean(val, b);
     return TRUE;
 }
 
@@ -402,6 +487,10 @@ char * g_english_strjoinv(char ** strv, const char * conjunction) {
     strv = g_strdupv(strv);
 
     length = g_strv_length(strv);
+
+    if (length == 1)
+       return stralloc(strv[0]);
+
     last = strv[length - 1];
     strv[length - 1] = NULL;
     
@@ -433,5 +522,54 @@ guint g_strv_length(gchar ** strv) {
     }
     return rval;
 }
-
 #endif /* GLIB_CHECK_VERSION(2.6.0) */
+
+#if !GLIB_CHECK_VERSION(2,4,0)
+void
+g_ptr_array_foreach (GPtrArray *array,
+                     GFunc      func,
+                     gpointer   user_data)
+{
+  guint i;
+
+  g_return_if_fail (array);
+
+  for (i = 0; i < array->len; i++)
+    (*func) (array->pdata[i], user_data);
+}
+#endif
+
+guint
+g_str_amanda_hash(
+       gconstpointer key)
+{
+    /* modified version of glib's hash function, copyright
+     * GLib Team and others 1997-2000. */
+    const char *p;
+    guint h = 0;
+
+    for (p = key; *p != '\0'; p++)
+        h = (h << 5) - h + (('_' == *p) ? '-' : g_ascii_tolower(*p));
+
+    return h;
+}
+
+gboolean
+g_str_amanda_equal(
+       gconstpointer v1,
+       gconstpointer v2)
+{
+    const gchar *p1 = v1, *p2 = v2;
+    while (*p1) {
+        /* letting '-' == '_' */
+        if (!('-' == *p1 || '_' == *p1) || !('-' == *p2 || '_' == *p2))
+            if (g_ascii_tolower(*p1) != g_ascii_tolower(*p2))
+                return FALSE;
+
+        p1++;
+        p2++;
+    }
+
+    /* p1 is at '\0' is p2 too? */
+    return *p2? FALSE : TRUE;
+}