[PATCH 1/3] Move get_problems_count() to its only user - "abrt-cli status"

Denys Vlasenko dvlasenk at redhat.com
Thu May 23 09:03:10 UTC 2013


Logic is not changed.

Signed-off-by: Denys Vlasenko <dvlasenk at redhat.com>
---
 src/cli/status.c          | 49 +++++++++++++++++++++++++++++++++++++++++++----
 src/include/problem_api.h |  8 --------
 src/lib/problem_api.c     | 38 ------------------------------------
 3 files changed, 45 insertions(+), 50 deletions(-)

diff --git a/src/cli/status.c b/src/cli/status.c
index 6f228c6..0dbcd1d 100644
--- a/src/cli/status.c
+++ b/src/cli/status.c
@@ -21,13 +21,52 @@
 #include <sys/types.h>
 #include "problem_api.h"
 
+struct time_range {
+    unsigned count;
+    unsigned long since;
+};
+
+static int count_dir_if_newer_than(struct dump_dir *dd, void *arg)
+{
+    struct time_range *me = arg;
+
+    char *time_str = dd_load_text(dd, FILENAME_LAST_OCCURRENCE);
+    long val = atol(time_str);
+    free(time_str);
+    if (val < me->since)
+        return 0;
+
+    me->count++;
+    return 0;
+}
+
+static void count_problems_in_dir(gpointer data, gpointer arg)
+{
+    char *path = data;
+    struct time_range *me = arg;
+
+    VERB2 log("scanning '%s' for problems since %lu", path, me->since);
+
+    for_each_problem_in_dir(path, getuid(), count_dir_if_newer_than, me);
+}
+
+static unsigned int count_problem_dirs(GList *paths, unsigned long since)
+{
+    struct time_range me;
+    me.count = 0;
+    me.since = since;
+
+    g_list_foreach(paths, count_problems_in_dir, &me);
+
+    return me.count;
+}
+
 int cmd_status(int argc, const char **argv)
 {
     const char *program_usage_string = _(
         "& status [DIR]..."
         );
 
-
     bool opt_bare = false;
     int opt_since = 0;
 
@@ -45,8 +84,12 @@ int cmd_status(int argc, const char **argv)
     GList *problem_dir_list = NULL;
     while (*argv)
         problem_dir_list = g_list_append(problem_dir_list, xstrdup(*argv++));
+    if (!problem_dir_list)
+        problem_dir_list = get_problem_storages();
+
+    unsigned int problem_count = count_problem_dirs(problem_dir_list, opt_since);
 
-    unsigned int problem_count = get_problems_count(problem_dir_list, opt_since);
+    list_free_with_free(problem_dir_list);
 
     /* show only if there is at least 1 problem or user set the -v */
     if (problem_count > 0 || g_verbose > 0)
@@ -57,7 +100,5 @@ int cmd_status(int argc, const char **argv)
             printf(_("ABRT has detected '%u' problem(s). (For more info run: $ abrt-cli list --full)\n"), problem_count);
     }
 
-    list_free_with_free(problem_dir_list);
-
     return 0;
 }
diff --git a/src/include/problem_api.h b/src/include/problem_api.h
index 803ec32..b8b6617 100644
--- a/src/include/problem_api.h
+++ b/src/include/problem_api.h
@@ -37,11 +37,3 @@ GList *get_problem_dirs_for_element_in_time(uid_t uid,
                                                       unsigned long timestamp_from,
                                                       unsigned long timestamp_to,
                                                       const char *dump_location);
-
-/* Counts all problems in given directories
- *
- * @paths[in] list of paths to scan (pass NULL to use the default problem directories)
- * @since[in]
- * @returns   count of problems
- */
-unsigned int get_problems_count(GList *paths, unsigned long since);
diff --git a/src/lib/problem_api.c b/src/lib/problem_api.c
index 894b725..2cd3336 100644
--- a/src/lib/problem_api.c
+++ b/src/lib/problem_api.c
@@ -166,41 +166,3 @@ GList *get_problem_storages(void)
 
     return pths;
 }
-
-/* get_problems_count and its helpers */
-
-typedef struct problem_count_info {
-    int problem_count;
-    unsigned long since;
-    unsigned long until;
-} problem_count_info_t;
-
-static void count_problems_in_dir(char *path, gpointer problem_counter)
-{
-    problem_count_info_t *pci = (problem_count_info_t *)problem_counter;
-    VERB2 log("scanning '%s' for problems since %lu", path, pci->since);
-    GList *problems = get_problem_dirs_for_element_in_time(getuid(), NULL /*don't filter by element*/, NULL, pci->since, pci->until, path);
-    pci->problem_count += g_list_length(problems);
-    list_free_with_free(problems);
-}
-
-unsigned int get_problems_count(GList *paths, unsigned long since)
-{
-    GList *pths = NULL;
-
-    if (paths == NULL)
-    {
-        paths = pths = get_problem_storages();
-    }
-
-    problem_count_info_t pci;
-    pci.problem_count = 0;
-    pci.since = since;
-    pci.until = time(NULL);
-
-    g_list_foreach(paths, (GFunc)count_problems_in_dir, &pci);
-
-    list_free_with_free(pths);
-
-    return pci.problem_count;
-}
-- 
1.8.1.4



More information about the Crash-catcher mailing list