3a204fb
From 75141a6a90fdc46a10c6c28db41c648b7fdfcd9c Mon Sep 17 00:00:00 2001
3a204fb
From: Jakub Filak <jfilak@redhat.com>
3a204fb
Date: Tue, 24 Mar 2015 18:05:59 +0100
3a204fb
Subject: [PATCH] problem_data: cache problem_item size
3a204fb
3a204fb
This is necessary for problem_data gotten from D-Bus where the
3a204fb
underlying files might not be directly accessible.
3a204fb
3a204fb
Signed-off-by: Jakub Filak <jfilak@redhat.com>
3a204fb
---
3a204fb
 src/include/problem_data.h |  8 ++++++++
3a204fb
 src/lib/problem_data.c     | 27 +++++++++++++++++++++++----
3a204fb
 2 files changed, 31 insertions(+), 4 deletions(-)
3a204fb
3a204fb
diff --git a/src/include/problem_data.h b/src/include/problem_data.h
3a204fb
index 7058198..9722562 100644
3a204fb
--- a/src/include/problem_data.h
3a204fb
+++ b/src/include/problem_data.h
3a204fb
@@ -46,9 +46,12 @@ enum {
3a204fb
     CD_FLAG_BIGTXT        = (1 << 6),
3a204fb
 };
3a204fb
 
3a204fb
+#define PROBLEM_ITEM_UNINITIALIZED_SIZE ((unsigned long)-1)
3a204fb
+
3a204fb
 struct problem_item {
3a204fb
     char    *content;
3a204fb
     unsigned flags;
3a204fb
+    unsigned long size;
3a204fb
     /* Used by UI for presenting "item allowed/not allowed" checkboxes: */
3a204fb
     int      selected_by_user;     /* 0 "don't know", -1 "no", 1 "yes" */
3a204fb
     int      allowed_by_reporter;  /* 0 "no", 1 "yes" */
3a204fb
@@ -82,6 +85,11 @@ void problem_data_add(problem_data_t *problem_data,
3a204fb
                 const char *name,
3a204fb
                 const char *content,
3a204fb
                 unsigned flags);
3a204fb
+struct problem_item *problem_data_add_ext(problem_data_t *problem_data,
3a204fb
+                const char *name,
3a204fb
+                const char *content,
3a204fb
+                unsigned flags,
3a204fb
+                unsigned long size);
3a204fb
 void problem_data_add_text_noteditable(problem_data_t *problem_data,
3a204fb
                 const char *name,
3a204fb
                 const char *content);
3a204fb
diff --git a/src/lib/problem_data.c b/src/lib/problem_data.c
3a204fb
index 697ecd1..ef76406 100644
3a204fb
--- a/src/lib/problem_data.c
3a204fb
+++ b/src/lib/problem_data.c
3a204fb
@@ -54,20 +54,27 @@ char *problem_item_format(struct problem_item *item)
3a204fb
 
3a204fb
 int problem_item_get_size(struct problem_item *item, unsigned long *size)
3a204fb
 {
3a204fb
+    if (item->size != PROBLEM_ITEM_UNINITIALIZED_SIZE)
3a204fb
+    {
3a204fb
+        *size = item->size;
3a204fb
+        return 0;
3a204fb
+    }
3a204fb
+
3a204fb
     if (item->flags & CD_FLAG_TXT)
3a204fb
     {
3a204fb
-        *size = strlen(item->content);
3a204fb
+        *size = item->size = strlen(item->content);
3a204fb
         return 0;
3a204fb
     }
3a204fb
 
3a204fb
     /* else if (item->flags & CD_FLAG_BIN) */
3a204fb
+
3a204fb
     struct stat statbuf;
3a204fb
     statbuf.st_size = 0;
3a204fb
 
3a204fb
     if (stat(item->content, &statbuf) != 0)
3a204fb
         return -errno;
3a204fb
 
3a204fb
-    *size = statbuf.st_size;
3a204fb
+    *size = item->size = statbuf.st_size;
3a204fb
     return 0;
3a204fb
 }
3a204fb
 
3a204fb
@@ -181,10 +188,11 @@ void problem_data_add_current_process_data(problem_data_t *pd)
3a204fb
     }
3a204fb
 }
3a204fb
 
3a204fb
-void problem_data_add(problem_data_t *problem_data,
3a204fb
+struct problem_item *problem_data_add_ext(problem_data_t *problem_data,
3a204fb
                 const char *name,
3a204fb
                 const char *content,
3a204fb
-                unsigned flags)
3a204fb
+                unsigned flags,
3a204fb
+                unsigned long size)
3a204fb
 {
3a204fb
     if (!(flags & CD_FLAG_BIN))
3a204fb
         flags |= CD_FLAG_TXT;
3a204fb
@@ -194,7 +202,18 @@ void problem_data_add(problem_data_t *problem_data,
3a204fb
     struct problem_item *item = (struct problem_item *)xzalloc(sizeof(*item));
3a204fb
     item->content = xstrdup(content);
3a204fb
     item->flags = flags;
3a204fb
+    item->size = size;
3a204fb
     g_hash_table_replace(problem_data, xstrdup(name), item);
3a204fb
+
3a204fb
+    return item;
3a204fb
+}
3a204fb
+
3a204fb
+void problem_data_add(problem_data_t *problem_data,
3a204fb
+                const char *name,
3a204fb
+                const char *content,
3a204fb
+                unsigned flags)
3a204fb
+{
3a204fb
+    problem_data_add_ext(problem_data, name, content, flags, PROBLEM_ITEM_UNINITIALIZED_SIZE);
3a204fb
 }
3a204fb
 
3a204fb
 void problem_data_add_text_noteditable(problem_data_t *problem_data,
3a204fb
-- 
3a204fb
2.1.0
3a204fb