84b277
From 4896a8507ea140f8f38bc88dc044b2e516a54cbf Mon Sep 17 00:00:00 2001
84b277
From: =?UTF-8?q?Zbigniew=20J=C4=99drzejewski-Szmek?= <zbyszek@in.waw.pl>
84b277
Date: Fri, 11 Apr 2014 08:44:55 -0400
84b277
Subject: [PATCH] journal: cleanup up error handling in update_catalog()
84b277
84b277
- Negative/positive errno mixup caused duplicates not to be detected properly.
84b277
  Now we get a warning about some duplicate entries in our own catalogs...
84b277
- Errors in update_catalog would be ignored, but they should not be.
84b277
84b277
(cherry-picked from e3b9d9c8027a7c4c55cf1614e0fe9423fad69e8f)
84b277
84b277
Resolves: #1147524
84b277
---
84b277
 src/journal/catalog.c      | 25 +++++++++++++------------
84b277
 src/journal/test-catalog.c |  3 ++-
84b277
 2 files changed, 15 insertions(+), 13 deletions(-)
84b277
84b277
diff --git a/src/journal/catalog.c b/src/journal/catalog.c
84b277
index 90ca008..e5342be 100644
84b277
--- a/src/journal/catalog.c
84b277
+++ b/src/journal/catalog.c
84b277
@@ -109,7 +109,7 @@ static int finish_item(
84b277
                 const char *payload) {
84b277
 
84b277
         ssize_t offset;
84b277
-        CatalogItem *i;
84b277
+        _cleanup_free_ CatalogItem *i = NULL;
84b277
         int r;
84b277
 
84b277
         assert(h);
84b277
@@ -129,13 +129,14 @@ static int finish_item(
84b277
         i->offset = htole64((uint64_t) offset);
84b277
 
84b277
         r = hashmap_put(h, i, i);
84b277
-        if (r == EEXIST) {
84b277
+        if (r == -EEXIST) {
84b277
                 log_warning("Duplicate entry for " SD_ID128_FORMAT_STR ".%s, ignoring.",
84b277
                             SD_ID128_FORMAT_VAL(id), language ? language : "C");
84b277
-                free(i);
84b277
                 return 0;
84b277
-        }
84b277
+        } else if (r < 0)
84b277
+                return r;
84b277
 
84b277
+        i = NULL;
84b277
         return 0;
84b277
 }
84b277
 
84b277
@@ -348,8 +349,8 @@ error:
84b277
 int catalog_update(const char* database, const char* root, const char* const* dirs) {
84b277
         _cleanup_strv_free_ char **files = NULL;
84b277
         char **f;
84b277
-        Hashmap *h;
84b277
         struct strbuf *sb = NULL;
84b277
+        _cleanup_hashmap_free_free_ Hashmap *h = NULL;
84b277
         _cleanup_free_ CatalogItem *items = NULL;
84b277
         CatalogItem *i;
84b277
         Iterator j;
84b277
@@ -371,13 +372,17 @@ int catalog_update(const char* database, const char* root, const char* const* di
84b277
         }
84b277
 
84b277
         STRV_FOREACH(f, files) {
84b277
-                log_debug("reading file '%s'", *f);
84b277
-                catalog_import_file(h, sb, *f);
84b277
+                log_debug("Reading file '%s'", *f);
84b277
+                r = catalog_import_file(h, sb, *f);
84b277
+                if (r < 0) {
84b277
+                        log_error("Failed to import file '%s': %s.",
84b277
+                                  *f, strerror(-r));
84b277
+                        goto finish;
84b277
+                }
84b277
         }
84b277
 
84b277
         if (hashmap_size(h) <= 0) {
84b277
                 log_info("No items in catalog.");
84b277
-                r = 0;
84b277
                 goto finish;
84b277
         } else
84b277
                 log_debug("Found %u items in catalog.", hashmap_size(h));
84b277
@@ -408,11 +413,7 @@ int catalog_update(const char* database, const char* root, const char* const* di
84b277
                 log_debug("%s: wrote %u items, with %zu bytes of strings, %ld total size.",
84b277
                           database, n, sb->len, r);
84b277
 
84b277
-        r = 0;
84b277
-
84b277
 finish:
84b277
-        if (h)
84b277
-                hashmap_free_free(h);
84b277
         if (sb)
84b277
                 strbuf_cleanup(sb);
84b277
 
84b277
diff --git a/src/journal/test-catalog.c b/src/journal/test-catalog.c
84b277
index 5db5bed..f021dbf 100644
84b277
--- a/src/journal/test-catalog.c
84b277
+++ b/src/journal/test-catalog.c
84b277
@@ -126,7 +126,8 @@ int main(int argc, char *argv[]) {
84b277
 
84b277
         setlocale(LC_ALL, "de_DE.UTF-8");
84b277
 
84b277
-        log_set_max_level(LOG_DEBUG);
84b277
+        log_parse_environment();
84b277
+        log_open();
84b277
 
84b277
         test_catalog_importing();
84b277