Blame SOURCES/0031-introduce-import-event-options-in-xml-event-definiti.patch

4b6aa8
From 90884eeb92527a8f0dbc2d90b3665b40ada3d426 Mon Sep 17 00:00:00 2001
4b6aa8
From: Jakub Filak <jfilak@redhat.com>
4b6aa8
Date: Fri, 21 Feb 2014 22:41:05 +0100
4b6aa8
Subject: [LIBREPORT PATCH 31/33] introduce import-event-options in xml event
4b6aa8
 definitions
4b6aa8
4b6aa8
Related to rhbz#1069111
4b6aa8
4b6aa8
Signed-off-by: Jakub Filak <jfilak@redhat.com>
4b6aa8
---
4b6aa8
 src/include/event_config.h |  1 +
4b6aa8
 src/lib/event_config.c     | 49 +++++++++++++++++++++++++++++++++++++---------
4b6aa8
 src/lib/event_xml_parser.c | 15 ++++++++++++++
4b6aa8
 3 files changed, 56 insertions(+), 9 deletions(-)
4b6aa8
4b6aa8
diff --git a/src/include/event_config.h b/src/include/event_config.h
4b6aa8
index 24e1eac..e2fcc23 100644
4b6aa8
--- a/src/include/event_config.h
4b6aa8
+++ b/src/include/event_config.h
4b6aa8
@@ -81,6 +81,7 @@ typedef struct
4b6aa8
     bool  ec_skip_review;
4b6aa8
     bool  ec_sending_sensitive_data;
4b6aa8
 
4b6aa8
+    GList *ec_imported_event_names;
4b6aa8
     GList *options;
4b6aa8
 } event_config_t;
4b6aa8
 
4b6aa8
diff --git a/src/lib/event_config.c b/src/lib/event_config.c
4b6aa8
index 6d12695..b25517d 100644
4b6aa8
--- a/src/lib/event_config.c
4b6aa8
+++ b/src/lib/event_config.c
4b6aa8
@@ -112,10 +112,8 @@ void free_event_config(event_config_t *p)
4b6aa8
     free(p->ec_exclude_items_by_default);
4b6aa8
     free(p->ec_include_items_by_default);
4b6aa8
     free(p->ec_exclude_items_always);
4b6aa8
-    GList *opt;
4b6aa8
-    for (opt = p->options; opt; opt = opt->next)
4b6aa8
-        free_event_option(opt->data);
4b6aa8
-    g_list_free(p->options);
4b6aa8
+    g_list_free_full(p->ec_imported_event_names, free);
4b6aa8
+    g_list_free_full(p->options, (GDestroyNotify)free_event_option);
4b6aa8
 
4b6aa8
     free(p);
4b6aa8
 }
4b6aa8
@@ -282,22 +280,54 @@ GList *export_event_config(const char *event_name)
4b6aa8
     event_config_t *config = get_event_config(event_name);
4b6aa8
     if (config)
4b6aa8
     {
4b6aa8
+        GList *imported = config->ec_imported_event_names;
4b6aa8
+        while (imported)
4b6aa8
+        {
4b6aa8
+            GList *exported = export_event_config(/*Event name*/imported->data);
4b6aa8
+            while (exported)
4b6aa8
+            {
4b6aa8
+                if (!g_list_find_custom(env_list, exported->data, (GCompareFunc)strcmp))
4b6aa8
+                    /* It is not necessary to make a copy of opt->eo_name */
4b6aa8
+                    /* since its memory is owned by event_option_t and it */
4b6aa8
+                    /* has global scope */
4b6aa8
+                    env_list = g_list_prepend(env_list, exported->data);
4b6aa8
+
4b6aa8
+                exported = g_list_remove_link(exported, exported);
4b6aa8
+            }
4b6aa8
+
4b6aa8
+            imported = g_list_next(imported);
4b6aa8
+        }
4b6aa8
+
4b6aa8
         GList *lopt;
4b6aa8
         for (lopt = config->options; lopt; lopt = lopt->next)
4b6aa8
         {
4b6aa8
             event_option_t *opt = lopt->data;
4b6aa8
             if (!opt->eo_value)
4b6aa8
                 continue;
4b6aa8
-            char *var_val = xasprintf("%s=%s", opt->eo_name, opt->eo_value);
4b6aa8
-            log_debug("Exporting '%s'", var_val);
4b6aa8
-            env_list = g_list_prepend(env_list, var_val);
4b6aa8
-            putenv(var_val);
4b6aa8
+
4b6aa8
+            log_debug("Exporting '%s=%s'", opt->eo_name, opt->eo_value);
4b6aa8
+
4b6aa8
+            /* Add the exported key only if it is not in the list */
4b6aa8
+            if (!g_list_find_custom(env_list, opt->eo_name, (GCompareFunc)strcmp))
4b6aa8
+                /* It is not necessary to make a copy of opt->eo_name */
4b6aa8
+                /* since its memory is owned by opt and it has global scope */
4b6aa8
+                env_list = g_list_prepend(env_list, opt->eo_name);
4b6aa8
+
4b6aa8
+            /* setenv() makes copies of strings */
4b6aa8
+            xsetenv(opt->eo_name, opt->eo_value);
4b6aa8
         }
4b6aa8
     }
4b6aa8
 
4b6aa8
     return env_list;
4b6aa8
 }
4b6aa8
 
4b6aa8
+/*
4b6aa8
+ * Goes through given list and calls unsetnev() for each list item.
4b6aa8
+ *
4b6aa8
+ * Accepts a list of 'const char *' type items which contains names of exported
4b6aa8
+ * environment variables and which was returned from export_event_config()
4b6aa8
+ * function.
4b6aa8
+ */
4b6aa8
 void unexport_event_config(GList *env_list)
4b6aa8
 {
4b6aa8
     while (env_list)
4b6aa8
@@ -305,8 +335,9 @@ void unexport_event_config(GList *env_list)
4b6aa8
         char *var_val = env_list->data;
4b6aa8
         log_debug("Unexporting '%s'", var_val);
4b6aa8
         safe_unsetenv(var_val);
4b6aa8
+
4b6aa8
+        /* The list doesn't own memory of values: see export_event_config() */
4b6aa8
         env_list = g_list_remove(env_list, var_val);
4b6aa8
-        free(var_val);
4b6aa8
     }
4b6aa8
 }
4b6aa8
 
4b6aa8
diff --git a/src/lib/event_xml_parser.c b/src/lib/event_xml_parser.c
4b6aa8
index 1f98158..a15f1e1 100644
4b6aa8
--- a/src/lib/event_xml_parser.c
4b6aa8
+++ b/src/lib/event_xml_parser.c
4b6aa8
@@ -40,6 +40,7 @@
4b6aa8
 #define EXCL_ALWAYS_ELEMENT     "exclude-items-always"
4b6aa8
 #define EXCL_BINARY_ELEMENT     "exclude-binary-items"
4b6aa8
 #define ADV_OPTIONS_ELEMENT     "advanced-options"
4b6aa8
+#define IMPORT_OPTIONS_ELEMENT  "import-event-options"
4b6aa8
 
4b6aa8
 typedef struct
4b6aa8
 {
4b6aa8
@@ -210,6 +211,20 @@ static void start_element(GMarkupParseContext *context,
4b6aa8
         }
4b6aa8
     }
4b6aa8
     else
4b6aa8
+    if (strcmp(element_name, IMPORT_OPTIONS_ELEMENT) == 0)
4b6aa8
+    {
4b6aa8
+        if (attribute_names[0] == NULL
4b6aa8
+            || attribute_names[1] != NULL
4b6aa8
+            || strcmp(attribute_names[0], "event") != 0)
4b6aa8
+        {
4b6aa8
+            error_msg("XML event configuration error: import-event-options element misses attribute 'event'");
4b6aa8
+            return;
4b6aa8
+        }
4b6aa8
+
4b6aa8
+        GList *head = parse_data->event_config.values->ec_imported_event_names;
4b6aa8
+        parse_data->event_config.values->ec_imported_event_names = g_list_append(head, xstrdup(attribute_values[0]));
4b6aa8
+    }
4b6aa8
+    else
4b6aa8
     if (strcmp(element_name, LABEL_ELEMENT) == 0
4b6aa8
      || strcmp(element_name, DESCRIPTION_ELEMENT) == 0
4b6aa8
      || strcmp(element_name, LONG_DESCR_ELEMENT) == 0
4b6aa8
-- 
4b6aa8
1.8.3.1
4b6aa8