chiark / gitweb /
journal: properly detect language specified in line
[elogind.git] / src / journal / catalog.c
index 7681af66299b18370bed94f6e6c324a7170631aa..f03357dedf9bc87ca84a28873d7e330282eae981 100644 (file)
@@ -39,6 +39,7 @@
 #include "conf-files.h"
 #include "mkdir.h"
 #include "catalog.h"
+#include "siphash24.h"
 
 const char * const catalog_file_dirs[] = {
         "/usr/local/lib/systemd/catalog/",
@@ -63,28 +64,21 @@ typedef struct CatalogItem {
         le64_t offset;
 } CatalogItem;
 
-unsigned catalog_hash_func(const void *p) {
+unsigned long catalog_hash_func(const void *p, const uint8_t hash_key[HASH_KEY_SIZE]) {
         const CatalogItem *i = p;
+        uint64_t u;
+        size_t l, sz;
+        void *v;
 
-        assert_cc(sizeof(unsigned) == sizeof(uint8_t)*4);
-
-        return (((unsigned) i->id.bytes[0] << 24) |
-                ((unsigned) i->id.bytes[1] << 16) |
-                ((unsigned) i->id.bytes[2] << 8) |
-                ((unsigned) i->id.bytes[3])) ^
-               (((unsigned) i->id.bytes[4] << 24) |
-                ((unsigned) i->id.bytes[5] << 16) |
-                ((unsigned) i->id.bytes[6] << 8) |
-                ((unsigned) i->id.bytes[7])) ^
-               (((unsigned) i->id.bytes[8] << 24) |
-                ((unsigned) i->id.bytes[9] << 16) |
-                ((unsigned) i->id.bytes[10] << 8) |
-                ((unsigned) i->id.bytes[11])) ^
-               (((unsigned) i->id.bytes[12] << 24) |
-                ((unsigned) i->id.bytes[13] << 16) |
-                ((unsigned) i->id.bytes[14] << 8) |
-                ((unsigned) i->id.bytes[15])) ^
-                string_hash_func(i->language);
+        l = strlen(i->language);
+        sz = sizeof(i->id) + l;
+        v = alloca(sz);
+
+        memcpy(mempcpy(v, &i->id, sizeof(i->id)), i->language, l);
+
+        siphash24((uint8_t*) &u, v, sz, hash_key);
+
+        return (unsigned long) u;
 }
 
 int catalog_compare_func(const void *a, const void *b) {
@@ -109,7 +103,7 @@ static int finish_item(
                 const char *payload) {
 
         ssize_t offset;
-        CatalogItem *i;
+        _cleanup_free_ CatalogItem *i = NULL;
         int r;
 
         assert(h);
@@ -125,17 +119,74 @@ static int finish_item(
                 return log_oom();
 
         i->id = id;
-        strscpy(i->language, sizeof(i->language), language);
+        if (language) {
+                assert(strlen(language) > 1 && strlen(language) < 32);
+                strcpy(i->language, language);
+        }
         i->offset = htole64((uint64_t) offset);
 
         r = hashmap_put(h, i, i);
-        if (r == EEXIST) {
+        if (r == -EEXIST) {
                 log_warning("Duplicate entry for " SD_ID128_FORMAT_STR ".%s, ignoring.",
                             SD_ID128_FORMAT_VAL(id), language ? language : "C");
-                free(i);
                 return 0;
+        } else if (r < 0)
+                return r;
+
+        i = NULL;
+        return 0;
+}
+
+int catalog_file_lang(const char* filename, char **lang) {
+        char *beg, *end, *_lang;
+
+        end = endswith(filename, ".catalog");
+        if (!end)
+                return 0;
+
+        beg = end - 1;
+        while (beg > filename && *beg != '.' && *beg != '/' && end - beg < 32)
+                beg --;
+
+        if (*beg != '.' || end <= beg + 1)
+                return 0;
+
+        _lang = strndup(beg + 1, end - beg - 1);
+        if (!_lang)
+                return -ENOMEM;
+
+        *lang = _lang;
+        return 1;
+}
+
+static int catalog_entry_lang(const char* filename, int line,
+                              const char* t, const char* deflang, char **lang) {
+        size_t c;
+
+        c = strlen(t);
+        if (c == 0) {
+                log_error("[%s:%u] Language too short.", filename, line);
+                return -EINVAL;
+        }
+        if (c > 31) {
+                log_error("[%s:%u] language too long.", filename, line);
+                return -EINVAL;
         }
 
+        if (deflang) {
+                if (streq(t, deflang)) {
+                        log_warning("[%s:%u] language specified unnecessarily",
+                                    filename, line);
+                        return 0;
+                } else
+                        log_warning("[%s:%u] language differs from default for file",
+                                    filename, line);
+        }
+
+        *lang = strdup(t);
+        if (!*lang)
+                        return -ENOMEM;
+
         return 0;
 }
 
@@ -144,7 +195,7 @@ int catalog_import_file(Hashmap *h, struct strbuf *sb, const char *path) {
         _cleanup_free_ char *payload = NULL;
         unsigned n = 0;
         sd_id128_t id;
-        char language[32];
+        _cleanup_free_ char *deflang = NULL, *lang = NULL;
         bool got_id = false, empty_line = true;
         int r;
 
@@ -158,6 +209,12 @@ int catalog_import_file(Hashmap *h, struct strbuf *sb, const char *path) {
                 return -errno;
         }
 
+        r = catalog_file_lang(path, &deflang);
+        if (r < 0)
+                log_error("Failed to determine language for file %s: %m", path);
+        if (r == 1)
+                log_debug("File %s has language %s.", path, deflang);
+
         for (;;) {
                 char line[LINE_MAX];
                 size_t a, b, c;
@@ -201,27 +258,21 @@ int catalog_import_file(Hashmap *h, struct strbuf *sb, const char *path) {
                         if (sd_id128_from_string(line + 2 + 1, &jd) >= 0) {
 
                                 if (got_id) {
-                                        r = finish_item(h, sb, id, language, payload);
+                                        r = finish_item(h, sb, id, lang ?: deflang, payload);
                                         if (r < 0)
                                                 return r;
+
+                                        free(lang);
+                                        lang = NULL;
                                 }
 
                                 if (with_language) {
                                         t = strstrip(line + 2 + 1 + 32 + 1);
 
-                                        c = strlen(t);
-                                        if (c <= 0) {
-                                                log_error("[%s:%u] Language too short.", path, n);
-                                                return -EINVAL;
-                                        }
-                                        if (c > sizeof(language) - 1) {
-                                                log_error("[%s:%u] language too long.", path, n);
-                                                return -EINVAL;
-                                        }
-
-                                        strscpy(language, sizeof(language), t);
-                                } else
-                                        language[0] = '\0';
+                                        r = catalog_entry_lang(path, n, t, deflang, &lang);
+                                        if (r < 0)
+                                                return r;
+                                }
 
                                 got_id = true;
                                 empty_line = false;
@@ -264,7 +315,7 @@ int catalog_import_file(Hashmap *h, struct strbuf *sb, const char *path) {
         }
 
         if (got_id) {
-                r = finish_item(h, sb, id, language, payload);
+                r = finish_item(h, sb, id, lang ?: deflang, payload);
                 if (r < 0)
                         return r;
         }
@@ -277,7 +328,7 @@ static long write_catalog(const char *database, Hashmap *h, struct strbuf *sb,
         CatalogHeader header;
         _cleanup_fclose_ FILE *w = NULL;
         int r;
-        char _cleanup_free_ *d, *p = NULL;
+        _cleanup_free_ char *d, *p = NULL;
         size_t k;
 
         d = dirname_malloc(database);
@@ -348,8 +399,8 @@ error:
 int catalog_update(const char* database, const char* root, const char* const* dirs) {
         _cleanup_strv_free_ char **files = NULL;
         char **f;
-        Hashmap *h;
         struct strbuf *sb = NULL;
+        _cleanup_hashmap_free_free_ Hashmap *h = NULL;
         _cleanup_free_ CatalogItem *items = NULL;
         CatalogItem *i;
         Iterator j;
@@ -371,13 +422,17 @@ int catalog_update(const char* database, const char* root, const char* const* di
         }
 
         STRV_FOREACH(f, files) {
-                log_debug("reading file '%s'", *f);
-                catalog_import_file(h, sb, *f);
+                log_debug("Reading file '%s'", *f);
+                r = catalog_import_file(h, sb, *f);
+                if (r < 0) {
+                        log_error("Failed to import file '%s': %s.",
+                                  *f, strerror(-r));
+                        goto finish;
+                }
         }
 
         if (hashmap_size(h) <= 0) {
                 log_info("No items in catalog.");
-                r = 0;
                 goto finish;
         } else
                 log_debug("Found %u items in catalog.", hashmap_size(h));
@@ -399,7 +454,7 @@ int catalog_update(const char* database, const char* root, const char* const* di
         }
 
         assert(n == hashmap_size(h));
-        qsort(items, n, sizeof(CatalogItem), catalog_compare_func);
+        qsort_safe(items, n, sizeof(CatalogItem), catalog_compare_func);
 
         r = write_catalog(database, h, sb, items, n);
         if (r < 0)
@@ -408,11 +463,7 @@ int catalog_update(const char* database, const char* root, const char* const* di
                 log_debug("%s: wrote %u items, with %zu bytes of strings, %ld total size.",
                           database, n, sb->len, r);
 
-        r = 0;
-
 finish:
-        if (h)
-                hashmap_free_free(h);
         if (sb)
                 strbuf_cleanup(sb);
 
@@ -434,18 +485,18 @@ static int open_mmap(const char *database, int *_fd, struct stat *_st, void **_p
                 return -errno;
 
         if (fstat(fd, &st) < 0) {
-                close_nointr_nofail(fd);
+                safe_close(fd);
                 return -errno;
         }
 
         if (st.st_size < (off_t) sizeof(CatalogHeader)) {
-                close_nointr_nofail(fd);
+                safe_close(fd);
                 return -EINVAL;
         }
 
         p = mmap(NULL, PAGE_ALIGN(st.st_size), PROT_READ, MAP_SHARED, fd, 0);
         if (p == MAP_FAILED) {
-                close_nointr_nofail(fd);
+                safe_close(fd);
                 return -errno;
         }
 
@@ -456,7 +507,7 @@ static int open_mmap(const char *database, int *_fd, struct stat *_st, void **_p
             h->incompatible_flags != 0 ||
             le64toh(h->n_items) <= 0 ||
             st.st_size < (off_t) (le64toh(h->header_size) + le64toh(h->catalog_item_size) * le64toh(h->n_items))) {
-                close_nointr_nofail(fd);
+                safe_close(fd);
                 munmap(p, st.st_size);
                 return -EBADMSG;
         }
@@ -627,7 +678,7 @@ int catalog_list_items(FILE *f, const char *database, bool oneline, char **items
         STRV_FOREACH(item, items) {
                 sd_id128_t id;
                 int k;
-                char _cleanup_free_ *msg = NULL;
+                _cleanup_free_ char *msg = NULL;
 
                 k = sd_id128_from_string(*item, &id);
                 if (k < 0) {