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

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