X-Git-Url: http://www.chiark.greenend.org.uk/ucgi/~ianmdlvl/git?p=elogind.git;a=blobdiff_plain;f=src%2Fjournal%2Fcatalog.c;h=f1702328419dfa52033b450d2dba054438a6f7a5;hp=3ed0b7ee81d1827628d0ce8a4607b9c5876541eb;hb=f8eeeaf9b783ebbab30672629abf3920db286811;hpb=03e334a1c7dc8c20c38902aa039440763acc9b17 diff --git a/src/journal/catalog.c b/src/journal/catalog.c index 3ed0b7ee8..f17023284 100644 --- a/src/journal/catalog.c +++ b/src/journal/catalog.c @@ -26,7 +26,6 @@ #include #include #include -#include #include "util.h" #include "log.h" @@ -64,7 +63,7 @@ typedef struct CatalogItem { le64_t offset; } CatalogItem; -unsigned long catalog_hash_func(const void *p, const uint8_t hash_key[HASH_KEY_SIZE]) { +static 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; @@ -81,7 +80,7 @@ unsigned long catalog_hash_func(const void *p, const uint8_t hash_key[HASH_KEY_S return (unsigned long) u; } -int catalog_compare_func(const void *a, const void *b) { +static int catalog_compare_func(const void *a, const void *b) { const CatalogItem *i = a, *j = b; unsigned k; @@ -95,6 +94,11 @@ int catalog_compare_func(const void *a, const void *b) { return strcmp(i->language, j->language); } +const struct hash_ops catalog_hash_ops = { + .hash = catalog_hash_func, + .compare = catalog_compare_func +}; + static int finish_item( Hashmap *h, struct strbuf *sb, @@ -103,7 +107,7 @@ static int finish_item( const char *payload) { ssize_t offset; - CatalogItem *i; + _cleanup_free_ CatalogItem *i = NULL; int r; assert(h); @@ -126,13 +130,14 @@ static int finish_item( 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; } @@ -158,6 +163,37 @@ int catalog_file_lang(const char* filename, char **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; +} + int catalog_import_file(Hashmap *h, struct strbuf *sb, const char *path) { _cleanup_fclose_ FILE *f = NULL; _cleanup_free_ char *payload = NULL; @@ -172,14 +208,12 @@ int catalog_import_file(Hashmap *h, struct strbuf *sb, const char *path) { assert(path); f = fopen(path, "re"); - if (!f) { - log_error("Failed to open file %s: %m", path); - return -errno; - } + if (!f) + return log_error_errno(errno, "Failed to open file %s: %m", path); r = catalog_file_lang(path, &deflang); if (r < 0) - log_error("Failed to determine language for file %s: %m", path); + log_error_errno(errno, "Failed to determine language for file %s: %m", path); if (r == 1) log_debug("File %s has language %s.", path, deflang); @@ -192,7 +226,7 @@ int catalog_import_file(Hashmap *h, struct strbuf *sb, const char *path) { if (feof(f)) break; - log_error("Failed to read file %s: %m", path); + log_error_errno(errno, "Failed to read file %s: %m", path); return -errno; } @@ -237,25 +271,9 @@ int catalog_import_file(Hashmap *h, struct strbuf *sb, const char *path) { 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 > 31) { - log_error("[%s:%u] language too long.", path, n); - return -EINVAL; - } - - if (deflang) { - log_warning("[%s:%u] language %s", path, n, - streq(t, deflang) ? - "specified unnecessarily" : - "differs from default for file"); - lang = strdup(t); - if (!lang) - return -ENOMEM; - } + r = catalog_entry_lang(path, n, t, deflang, &lang); + if (r < 0) + return r; } got_id = true; @@ -320,17 +338,13 @@ static long write_catalog(const char *database, Hashmap *h, struct strbuf *sb, return log_oom(); r = mkdir_p(d, 0775); - if (r < 0) { - log_error("Recursive mkdir %s: %s", d, strerror(-r)); - return r; - } + if (r < 0) + return log_error_errno(r, "Recursive mkdir %s: %m", d); r = fopen_temporary(database, &w, &p); - if (r < 0) { - log_error("Failed to open database for writing: %s: %s", - database, strerror(-r)); - return r; - } + if (r < 0) + return log_error_errno(r, "Failed to open database for writing: %s: %m", + database); zero(header); memcpy(header.signature, CATALOG_SIGNATURE, sizeof(header.signature)); @@ -368,7 +382,7 @@ static long write_catalog(const char *database, Hashmap *h, struct strbuf *sb, fchmod(fileno(w), 0644); if (rename(p, database) < 0) { - log_error("rename (%s -> %s) failed: %m", p, database); + log_error_errno(errno, "rename (%s -> %s) failed: %m", p, database); r = -errno; goto error; } @@ -383,15 +397,15 @@ 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; unsigned n; long r; - h = hashmap_new(catalog_hash_func, catalog_compare_func); + h = hashmap_new(&catalog_hash_ops); sb = strbuf_new(); if (!h || !sb) { @@ -401,18 +415,22 @@ int catalog_update(const char* database, const char* root, const char* const* di r = conf_files_list_strv(&files, ".catalog", root, dirs); if (r < 0) { - log_error("Failed to get catalog files: %s", strerror(-r)); + log_error_errno(r, "Failed to get catalog files: %m"); goto finish; } 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)); @@ -438,16 +456,12 @@ int catalog_update(const char* database, const char* root, const char* const* di r = write_catalog(database, h, sb, items, n); if (r < 0) - log_error("Failed to write %s: %s", database, strerror(-r)); + log_error_errno(r, "Failed to write %s: %m", database); else 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); @@ -666,8 +680,8 @@ int catalog_list_items(FILE *f, const char *database, bool oneline, char **items k = sd_id128_from_string(*item, &id); if (k < 0) { - log_error("Failed to parse id128 '%s': %s", - *item, strerror(-k)); + log_error_errno(k, "Failed to parse id128 '%s': %m", + *item); if (r == 0) r = k; continue;