Blame SOURCES/0146-lib-get-possible-events-for-problem_data_t.patch

28bab8
From fc028b3417349fd60a2ddd1aff1127a417df512b Mon Sep 17 00:00:00 2001
28bab8
From: Jakub Filak <jfilak@redhat.com>
28bab8
Date: Thu, 2 Jul 2015 15:04:06 +0200
28bab8
Subject: [PATCH] lib: get possible events for problem_data_t
28bab8
28bab8
Certainly useful when we have no file system access to dump directories
28bab8
and we only get a problem data via a D-Bus service.
28bab8
28bab8
Related: #1224984
28bab8
28bab8
Signed-off-by: Jakub Filak <jfilak@redhat.com>
28bab8
---
28bab8
 src/include/run_event.h |  8 +++++++
28bab8
 src/lib/glib_support.c  |  2 +-
28bab8
 src/lib/run_event.c     | 56 +++++++++++++++++++++++++++++++++++--------------
28bab8
 3 files changed, 49 insertions(+), 17 deletions(-)
28bab8
28bab8
diff --git a/src/include/run_event.h b/src/include/run_event.h
28bab8
index 7579e8f..bc43d4f 100644
28bab8
--- a/src/include/run_event.h
28bab8
+++ b/src/include/run_event.h
28bab8
@@ -186,6 +186,9 @@ int run_event_on_problem_data(struct run_event_state *state, problem_data_t *dat
28bab8
  */
28bab8
 char *list_possible_events(struct dump_dir *dd, const char *dump_dir_name, const char *pfx);
28bab8
 
28bab8
+/* Like list_possible_events but accepts problem_data_t */
28bab8
+char *list_possible_events_problem_data(problem_data_t *pd, const char *dump_dir_name, const char *pfx);
28bab8
+
28bab8
 /*
28bab8
  * Returns a list of possible events for given problem directory
28bab8
  *
28bab8
@@ -195,6 +198,11 @@ char *list_possible_events(struct dump_dir *dd, const char *dump_dir_name, const
28bab8
 GList *list_possible_events_glist(const char *problem_dir_name,
28bab8
                                   const char *pfx);
28bab8
 
28bab8
+/* Like list_possible_events_glist but accepts problem_data_t */
28bab8
+GList *list_possible_events_problem_data_glist(problem_data_t *pd,
28bab8
+                                  const char *problem_dir_name,
28bab8
+                                  const char *pfx);
28bab8
+
28bab8
 /* Command line run event callback implemenetation */
28bab8
 
28bab8
 /*
28bab8
diff --git a/src/lib/glib_support.c b/src/lib/glib_support.c
28bab8
index 02c2dfd..2751b0c 100644
28bab8
--- a/src/lib/glib_support.c
28bab8
+++ b/src/lib/glib_support.c
28bab8
@@ -92,7 +92,7 @@ GList *parse_list(const char* list)
28bab8
 
28bab8
     char *tmp_list = xstrdup(list);
28bab8
 
28bab8
-    GList *l = parse_delimited_list(list, LIST_DELIMITER);
28bab8
+    GList *l = parse_delimited_list(tmp_list, LIST_DELIMITER);
28bab8
 
28bab8
     free(tmp_list);
28bab8
 
28bab8
diff --git a/src/lib/run_event.c b/src/lib/run_event.c
28bab8
index a56cf88..252c6bc 100644
28bab8
--- a/src/lib/run_event.c
28bab8
+++ b/src/lib/run_event.c
28bab8
@@ -298,11 +298,17 @@ static int regcmp_lines(char *val, const char *regex)
28bab8
 static char* pop_next_command(GList **pp_rule_list,
28bab8
         char **pp_event_name,    /* reports EVENT value thru this, if not NULL on entry */
28bab8
         struct dump_dir **pp_dd, /* use *pp_dd for access to dump dir, if non-NULL */
28bab8
+        problem_data_t *pd,     /* use *pp for access to problem data, if non-NULL */
28bab8
         const char *dump_dir_name,
28bab8
         const char *pfx,
28bab8
         unsigned pfx_len
28bab8
 )
28bab8
 {
28bab8
+    /* It is an error to pass both, but we can recover from it and use only
28bab8
+     * problem_data_t in that case */
28bab8
+    if (pp_dd != NULL && pd != NULL)
28bab8
+        error_msg("BUG: both dump dir and problem data passed to %s()", __func__);
28bab8
+
28bab8
     char *command = NULL;
28bab8
     struct dump_dir *dd = pp_dd ? *pp_dd : NULL;
28bab8
 
28bab8
@@ -331,7 +337,7 @@ static char* pop_next_command(GList **pp_rule_list,
28bab8
             else
28bab8
             {
28bab8
                 /* Read from dump dir and compare */
28bab8
-                if (!dd)
28bab8
+                if (!dd && pd == NULL)
28bab8
                 {
28bab8
                     /* Without dir to match, we assume match for all conditions */
28bab8
                     if (!dump_dir_name)
28bab8
@@ -349,10 +355,15 @@ static char* pop_next_command(GList **pp_rule_list,
28bab8
                 /* Is it "VAR!=VAL"? */
28bab8
                 int inverted = (eq_sign > cond_str && eq_sign[-1] == '!');
28bab8
                 char *var_name = xstrndup(cond_str, eq_sign - cond_str - (regex|inverted));
28bab8
-                char *real_val = dd_load_text_ext(dd, var_name, DD_FAIL_QUIETLY_ENOENT);
28bab8
+                char *real_val = NULL;
28bab8
+                char *free_me = NULL;
28bab8
+                if (pd == NULL)
28bab8
+                    free_me = real_val = dd_load_text_ext(dd, var_name, DD_FAIL_QUIETLY_ENOENT);
28bab8
+                else
28bab8
+                    real_val = problem_data_get_content_or_NULL(pd, var_name);
28bab8
                 free(var_name);
28bab8
                 int vals_differ = regex ? regcmp_lines(real_val, eq_sign + 1) : strcmp(real_val, eq_sign + 1);
28bab8
-                free(real_val);
28bab8
+                free(free_me);
28bab8
                 if (inverted)
28bab8
                     vals_differ = !vals_differ;
28bab8
 
28bab8
@@ -422,6 +433,7 @@ int spawn_next_command(struct run_event_state *state,
28bab8
     char *cmd = pop_next_command(&state->rule_list,
28bab8
                 NULL,          /* don't return event_name */
28bab8
                 NULL,          /* NULL dd: we match by... */
28bab8
+                NULL,          /* no problem data */
28bab8
                 dump_dir_name, /* ...dirname */
28bab8
                 event, strlen(event)+1 /* for this event name exactly (not prefix) */
28bab8
     );
28bab8
@@ -648,7 +660,8 @@ int run_event_on_problem_data(struct run_event_state *state, problem_data_t *dat
28bab8
     return r;
28bab8
 }
28bab8
 
28bab8
-char *list_possible_events(struct dump_dir *dd, const char *dump_dir_name, const char *pfx)
28bab8
+
28bab8
+static char *_list_possible_events(struct dump_dir **dd, problem_data_t *pd, const char *dump_dir_name, const char *pfx)
28bab8
 {
28bab8
     struct strbuf *result = strbuf_new();
28bab8
 
28bab8
@@ -661,7 +674,8 @@ char *list_possible_events(struct dump_dir *dd, const char *dump_dir_name, const
28bab8
         char *event_name = NULL;
28bab8
         char *cmd = pop_next_command(&rule_list,
28bab8
                 &event_name,       /* return event_name */
28bab8
-                (dd ? &dd : NULL), /* match this dd... */
28bab8
+                dd,                /* match this dd... */
28bab8
+                pd,                /* no problem data */
28bab8
                 dump_dir_name,     /* ...or if NULL, this dirname */
28bab8
                 pfx, pfx_len       /* for events with this prefix */
28bab8
         );
28bab8
@@ -695,24 +709,34 @@ char *list_possible_events(struct dump_dir *dd, const char *dump_dir_name, const
28bab8
     return strbuf_free_nobuf(result);
28bab8
 }
28bab8
 
28bab8
+char *list_possible_events(struct dump_dir *dd, const char *dump_dir_name, const char *pfx)
28bab8
+{
28bab8
+    return _list_possible_events((dd ? &dd : NULL), NULL, dump_dir_name, pfx);
28bab8
+}
28bab8
+
28bab8
+char *list_possible_events_problem_data(problem_data_t *pd, const char *dump_dir_name, const char *pfx)
28bab8
+{
28bab8
+    return _list_possible_events(NULL, pd, dump_dir_name, pfx);
28bab8
+}
28bab8
+
28bab8
 GList *list_possible_events_glist(const char *problem_dir_name,
28bab8
                                   const char *pfx)
28bab8
 {
28bab8
     struct dump_dir *dd = dd_opendir(problem_dir_name, DD_OPEN_READONLY);
28bab8
-    GList *l = NULL;
28bab8
     char *events = list_possible_events(dd, problem_dir_name, pfx);
28bab8
-    char *start = events;
28bab8
-    char *end = strchr(events, '\n');
28bab8
+    GList *l = parse_delimited_list(events, "\n");
28bab8
+    dd_close(dd);
28bab8
+    free(events);
28bab8
 
28bab8
-    while(end)
28bab8
-    {
28bab8
-        *end = '\0';
28bab8
-        l = g_list_append(l, xstrdup(start));
28bab8
-        start = end + 1;
28bab8
-        end = strchr(start, '\n');
28bab8
-    }
28bab8
+    return l;
28bab8
+}
28bab8
 
28bab8
-    dd_close(dd);
28bab8
+GList *list_possible_events_problem_data_glist(problem_data_t *pd,
28bab8
+                                  const char *problem_dir_name,
28bab8
+                                  const char *pfx)
28bab8
+{
28bab8
+    char *events = list_possible_events_problem_data(pd, problem_dir_name, pfx);
28bab8
+    GList *l = parse_delimited_list(events, "\n");
28bab8
     free(events);
28bab8
 
28bab8
     return l;
28bab8
-- 
28bab8
2.4.3
28bab8